perm filename LSPDC1.S78[206,LSP] blob
sn#343708 filedate 1978-03-22 generic text, type T, neo UTF8
/LMAR=0/XLINE=3/FONT#0=BAXL30[FNT,CLT]/FONT#1=BAXM30/FONT#2=BAXB30[FNT,CLT]/FONT#3=SUB/FONT#4=SUP/FONT#5=NGR25/FONT#6=NGR20/FONT#7=SYMB30[FNT,CLT]/FONT#8=FIX25/FONT#11=GRFX25/FONT#12=GRFX35
␈↓ ↓H␈↓α␈↓ ε%LISP
␈↓ ↓H␈↓α␈↓ ¬⊃Programming and Proving
␈↓ ↓H␈↓α␈↓ ∧<by John McCarthy and Carolyn Talcott
␈↓ ↓H␈↓α␈↓ ¬AStanford University
␈↓ ↓H␈↓α␈↓ ¬RCopyright ␈↓π@␈↓α 1978
␈↓ ↓H␈↓α␈↓ ∧RJohn McCarthy and Carolyn Talcott
␈↓ ↓H␈↓α␈↓ ∧⊂This version printed at 3:06 on March 22, 1978.
␈↓ ↓H␈↓␈↓ εH␈↓ ?i
␈↓ ↓H␈↓α␈↓ ¬NTable of Contents
␈↓ ↓H␈↓␈↓ Page
␈↓ ↓H␈↓INTRODUCTION
␈↓ ↓H␈↓I␈↓ α_INTRODUCTION TO LISP
␈↓ ↓H␈↓␈↓ α81␈↓ αxLists.␈↓ βx . . . . . . . . . . . . . . . . . . . . . . . . .␈↓ ≠ 1
␈↓ ↓H␈↓␈↓ α82␈↓ αxAtoms.␈↓ βx . . . . . . . . . . . . . . . . . . . . . . . . .␈↓ ≠ 2
␈↓ ↓H␈↓␈↓ α83␈↓ αxList structures.␈↓ ∧8 . . . . . . . . . . . . . . . . . . . . . . . .␈↓ ≠ 3
␈↓ ↓H␈↓␈↓ α84␈↓ αxS-expressions.␈↓ ∧8 . . . . . . . . . . . . . . . . . . . . . . . .␈↓ ≠ 4
␈↓ ↓H␈↓␈↓ α85␈↓ αxThe basic functions and predicates of LISP.␈↓ π8 . . . . . . . . . . . . .␈↓ ≠ 6
␈↓ ↓H␈↓␈↓ α86␈↓ αxConditional expressions.␈↓ ¬8 . . . . . . . . . . . . . . . . . . . .␈↓ ≠ 9
␈↓ ↓H␈↓␈↓ α87␈↓ αxPropositional expressions␈↓ ¬x . . . . . . . . . . . . . . . . . .␈↓ 11
␈↓ ↓H␈↓␈↓ α88␈↓ αxRecursive function definitions.␈↓ ¬x . . . . . . . . . . . . . . . . . .␈↓ 12
␈↓ ↓H␈↓␈↓ α89␈↓ αxLambda expressions and functions with functions as arguments.␈↓ 8 . . . . .␈↓ 17
␈↓ ↓H␈↓␈↓ α810␈↓ αxLabel.␈↓ βx . . . . . . . . . . . . . . . . . . . . . . . . .␈↓ 21
␈↓ ↓H␈↓␈↓ α811␈↓ αxNumerical computation.␈↓ ¬8 . . . . . . . . . . . . . . . . . . . .␈↓ 21
␈↓ ↓H␈↓␈↓ α812␈↓ αxThe function ␈↓↓eval.␈↓␈↓ ∧x . . . . . . . . . . . . . . . . . . . . .␈↓ 24
␈↓ ↓H␈↓II␈↓ α_HOW TO WRITE RECURSIVE FUNCTION DEFINITIONS
␈↓ ↓H␈↓␈↓ α81␈↓ αxStatic and dynamic ways of programming.␈↓ π8 . . . . . . . . . . . . .␈↓ 28
␈↓ ↓H␈↓␈↓ α82␈↓ αxSimple list recursion.␈↓ ∧x . . . . . . . . . . . . . . . . . . . . .␈↓ 29
␈↓ ↓H␈↓␈↓ α83␈↓ αxSimple S-expression recursion.␈↓ ¬x . . . . . . . . . . . . . . . . . .␈↓ 30
␈↓ ↓H␈↓␈↓ α84␈↓ αxOther structural recursions.␈↓ ¬x . . . . . . . . . . . . . . . . . .␈↓ 31
␈↓ ↓H␈↓␈↓ α85␈↓ αxTree search recursion.␈↓ ¬8 . . . . . . . . . . . . . . . . . . . .␈↓ 32
␈↓ ↓H␈↓␈↓ α86␈↓ αxGame trees.␈↓ ∧8 . . . . . . . . . . . . . . . . . . . . . . .␈↓ 35
␈↓ ↓H␈↓ii␈↓ ¬RTable of Contents␈↓ H
␈↓ ↓H␈↓␈↓ α87␈↓ αxThe hidden board trick.␈↓ ¬8 . . . . . . . . . . . . . . . . . . . .␈↓ 43
␈↓ ↓H␈↓␈↓ α88␈↓ αx2-dimensional Tic Tac Toe.␈↓ ¬x . . . . . . . . . . . . . . . . . .␈↓ 44
␈↓ ↓H␈↓III␈↓ α_PROVING LISP PROGRAMS CORRECT
␈↓ ↓H␈↓␈↓ α81␈↓ αxIntroductory Example.␈↓ ¬8 . . . . . . . . . . . . . . . . . . . .␈↓ 49
␈↓ ↓H␈↓␈↓ α82␈↓ αxFirst Order Logic with Conditional Forms and Lambda-expressions.␈↓ x . . . .␈↓ 51
␈↓ ↓H␈↓␈↓ α83␈↓ αxConditional Expressions.␈↓ ¬8 . . . . . . . . . . . . . . . . . . . .␈↓ 54
␈↓ ↓H␈↓␈↓ α84␈↓ αxLambda-expressions.␈↓ ¬8 . . . . . . . . . . . . . . . . . . . .␈↓ 55
␈↓ ↓H␈↓␈↓ α85␈↓ αxFirst Order Axioms for LISP.␈↓ ¬x . . . . . . . . . . . . . . . . . .␈↓ 56
␈↓ ↓H␈↓␈↓ α86␈↓ αxAxiom Schemas of Induction.␈↓ ¬x . . . . . . . . . . . . . . . . . .␈↓ 58
␈↓ ↓H␈↓␈↓ α87␈↓ αxA Simple Example.␈↓ ∧x . . . . . . . . . . . . . . . . . . . . .␈↓ 59
␈↓ ↓H␈↓␈↓ α88␈↓ αxPseudo-logic.␈↓ ∧8 . . . . . . . . . . . . . . . . . . . . . . .␈↓ 62
␈↓ ↓H␈↓␈↓ α89␈↓ αxAn Extended Example.␈↓ ¬8 . . . . . . . . . . . . . . . . . . . .␈↓ 65
␈↓ ↓H␈↓␈↓ α810␈↓ αxFunctionals and Least Fixedpoints.␈↓ ε8 . . . . . . . . . . . . . . . .␈↓ 71
␈↓ ↓H␈↓␈↓ α811␈↓ αxThe Minimization Schema.␈↓ ¬x . . . . . . . . . . . . . . . . . .␈↓ 74
␈↓ ↓H␈↓IV␈↓ α_IMPURE PROGRAMS AND UNCLEAN PROGRAMS
␈↓ ↓H␈↓␈↓ α81␈↓ αxSequential (ALGOL-like) programs.␈↓ εx . . . . . . . . . . . . . .␈↓ 80
␈↓ ↓H␈↓␈↓ α82␈↓ αxPseudo-functions that modify list structures␈↓ π8 . . . . . . . . . . . . .␈↓ 85
␈↓ ↓H␈↓␈↓ α83␈↓ αxRe-entrant List Structure␈↓ ¬8 . . . . . . . . . . . . . . . . . . . .␈↓ 86
␈↓ ↓H␈↓␈↓ α84␈↓ αxProperty Lists␈↓ ∧8 . . . . . . . . . . . . . . . . . . . . . . .␈↓ 89
␈↓ ↓H␈↓␈↓ α85␈↓ αxInput and output.␈↓ ∧x . . . . . . . . . . . . . . . . . . . . .␈↓ 91
␈↓ ↓H␈↓V␈↓ α_TRANSFORMING LISP PROGRAMS
␈↓ ↓H␈↓␈↓ α81␈↓ αxPurifying LISP programs.␈↓ ¬x . . . . . . . . . . . . . . . . . .␈↓ 94
␈↓ ↓H␈↓␈↓ α82␈↓ αxDerived programs␈↓ ∧x . . . . . . . . . . . . . . . . . . . . .␈↓ 97
␈↓ ↓H␈↓␈↓ α83␈↓ αxSubstantial exercises␈↓ ∧x . . . . . . . . . . . . . . . . . . . . .␈↓
⎇ 101
␈↓ ↓H␈↓␈↓ ¬RTable of Contents␈↓ -iii
␈↓ ↓H␈↓VI␈↓ α_ABSTRACT SYNTAX
␈↓ ↓H␈↓␈↓ α81␈↓ αxWhat is Abstract Syntax?␈↓ ¬8 . . . . . . . . . . . . . . . . . . .␈↓
⎇ 102
␈↓ ↓H␈↓␈↓ α82␈↓ αxCorrectness of a Compiler for Arithmetic Expressions␈↓ λ8 . . . . . . . . .␈↓
⎇ 106
␈↓ ↓H␈↓␈↓ α83␈↓ αxThe source language.␈↓ ¬8 . . . . . . . . . . . . . . . . . . .␈↓
⎇ 107
␈↓ ↓H␈↓␈↓ α84␈↓ αxThe object language.␈↓ ¬8 . . . . . . . . . . . . . . . . . . .␈↓
⎇ 108
␈↓ ↓H␈↓␈↓ α85␈↓ αxThe compiler.␈↓ ∧8 . . . . . . . . . . . . . . . . . . . . . . .␈↓
⎇ 110
␈↓ ↓H␈↓␈↓ α86␈↓ αxProof of Theorem 1.␈↓ ∧x . . . . . . . . . . . . . . . . . . . . .␈↓
⎇ 111
␈↓ ↓H␈↓␈↓ α87␈↓ αxRemarks.␈↓ βx . . . . . . . . . . . . . . . . . . . . . . . . .␈↓
⎇ 113
␈↓ ↓H␈↓␈↓ α88␈↓ αxSome substantial exercises.␈↓ ¬x . . . . . . . . . . . . . . . . .␈↓
⎇ 114
␈↓ ↓H␈↓VII␈↓ α_COMPILING IN LISP
␈↓ ↓H␈↓␈↓ α81␈↓ αxSome facts about the PDP-10.␈↓ ¬x . . . . . . . . . . . . . . . . .␈↓
⎇ 116
␈↓ ↓H␈↓␈↓ α82␈↓ αxCode produced by LISP compilers.␈↓ ε8 . . . . . . . . . . . . . . . .␈↓
⎇ 118
␈↓ ↓H␈↓␈↓ α83␈↓ αxLCOM0.␈↓ βx . . . . . . . . . . . . . . . . . . . . . . . . .␈↓
⎇ 123
␈↓ ↓H␈↓␈↓ α84␈↓ αxLCOM4.␈↓ βx . . . . . . . . . . . . . . . . . . . . . . . . .␈↓
⎇ 127
␈↓ ↓H␈↓VIII␈↓ α_COMPUTABILITY
␈↓ ↓H␈↓␈↓ α81␈↓ αxA universal LISP function.␈↓ ¬x . . . . . . . . . . . . . . . . .␈↓
⎇ 133
␈↓ ↓H␈↓␈↓ α82␈↓ αxNon-computability.␈↓ ∧x . . . . . . . . . . . . . . . . . . . . .␈↓
⎇ 134
␈↓ ↓H␈↓BIBLIOGRAPHY
␈↓ ↓H␈↓A␈↓ α_Examples of Computer-checked proofs
␈↓ ↓H␈↓␈↓ α81␈↓ αxIntroduction to FOL␈↓ ∧x . . . . . . . . . . . . . . . . . . . . . .␈↓ ! i
␈↓ ↓H␈↓iv␈↓ ¬RTable of Contents␈↓ H
␈↓ ↓H␈↓B␈↓ α_LISP Compilers
␈↓ ↓H␈↓␈↓ α81␈↓ αxMACLISP listing of LCOM0␈↓ ¬x . . . . . . . . . . . . . . . . .␈↓
w xxv
␈↓ ↓H␈↓␈↓ α82␈↓ αxMACLISP listing of LCOM4␈↓ ¬x . . . . . . . . . . . . . . . . .␈↓
e xxvii
␈↓ ↓H␈↓FUNCTION INDEX
␈↓ ↓H␈↓␈↓ εH␈↓ ?i
␈↓ ↓H␈↓α␈↓ ¬IINTRODUCTION
␈↓ ↓H␈↓ This␈α
book␈α
covers␈α∞recursive␈α
programming␈α
in␈α∞LISP,␈α
computation␈α
with␈α∞symbolic␈α
expressions
␈↓ ↓H␈↓represented␈α∀by␈α∀LISP␈α∀S-expressions,␈α∀representation␈α∀of␈α∀S-expressions␈α∀by␈α∀list␈α∀structure␈α∀in␈α∀the
␈↓ ↓H␈↓memory␈α⊃of␈α⊃a␈α⊃computer,␈α⊃and␈α⊃techniques␈α⊂for␈α⊃mathematically␈α⊃proving␈α⊃that␈α⊃programs␈α⊃meet␈α⊂their
␈↓ ↓H␈↓specifications. We cover the following main topics.
␈↓ ↓H␈↓1.␈α∩Writing␈α∪recursive␈α∩programs.␈α∪ Most␈α∩students␈α∩will␈α∪have␈α∩already␈α∪learned␈α∩to␈α∪write␈α∩sequential
␈↓ ↓H␈↓programs␈α∂which␈α⊂change␈α∂an␈α⊂initial␈α∂state␈α⊂of␈α∂a␈α⊂computation␈α∂to␈α⊂reach␈α∂a␈α⊂state␈α∂satisfying␈α⊂a␈α∂desired
␈↓ ↓H␈↓condition.␈α Recursive␈αprogramming␈αrequires␈αinventing␈αfunctions␈αthat␈αgive␈αthe␈αanswer␈αin␈αterms␈αof
␈↓ ↓H␈↓the␈α
initial␈αinformation␈α
by␈αbuilding␈α
it␈α
up␈αfrom␈α
simpler␈αfunctions␈α
and␈α
simpler␈αcases␈α
of␈αitself.␈α
Either
␈↓ ↓H␈↓kind␈α∂of␈α∂program␈α∂is␈α∂universal,␈α∂but␈α∂in␈α∞practice␈α∂the␈α∂techniques␈α∂are␈α∂complementary.␈α∂ The␈α∂kind␈α∞of
␈↓ ↓H␈↓recursion␈αthat␈αmakes␈αgood␈αprograms␈αis␈α␈↓↓conditional␈αexpression␈αrecursion␈↓␈αwhich␈αdiffers␈αin␈αimportant
␈↓ ↓H␈↓respects from the recursive definitions familiar to mathematicians.
␈↓ ↓H␈↓2.␈α
Computing␈α
with␈α
symbolic␈α
expressions␈α
represented␈α
by␈α
list␈α
structure␈α
in␈α
the␈α
memory␈α
of␈α
a␈α
computer.
␈↓ ↓H␈↓The␈αLISP␈αS-expressions␈αform␈αa␈αdata␈αdomain␈αwith␈αa␈αsimple␈αand␈αregular␈αstructure,␈αand␈αimportant
␈↓ ↓H␈↓computations␈α→with␈α→symbolic␈α→expressions␈α→are␈α→readily␈α→represented␈α→as␈α→functions␈α→defined␈α→by
␈↓ ↓H␈↓conditional␈α∩expression␈α∩recursion.␈α∩ The␈α∩examples␈α∩are␈α∩taken␈α∩from␈α∩tree␈α∩search,␈α∩computing␈α∩with
␈↓ ↓H␈↓algebraic␈αand␈αother␈αexpressions␈αinvolving␈αelementary␈αfunctions,␈αpattern␈αmatching,␈αand␈αcompiling,
␈↓ ↓H␈↓interpreting␈α
and␈α
transforming␈α
recursive␈αprograms.␈α
Recursive␈α
programming␈α
is␈α
the␈αmain␈α
technique,
␈↓ ↓H␈↓but the use of sequential programs is also discussed.
␈↓ ↓H␈↓ The␈α
student␈α∞who␈α
has␈α∞completed␈α
a␈α∞course␈α
based␈α
on␈α∞this␈α
book␈α∞should␈α
be␈α∞able␈α
to␈α∞use␈α
LISP
␈↓ ↓H␈↓proficiently for symbolic computation and in artificial intelligence applications.
␈↓ ↓H␈↓3.␈α_Proving␈α_programs␈α↔correct.␈α_ A␈α_major␈α↔applied␈α_task␈α_of␈α↔computer␈α_science␈α_is␈α_to␈α↔develop
␈↓ ↓H␈↓mathematical␈α
theory␈α
of␈α
computation␈α
to␈α
the␈αpoint␈α
where␈α
a␈α
program␈α
is␈α
not␈α
considered␈αcomplete␈α
until
␈↓ ↓H␈↓it␈αhas␈α
been␈αproven␈α
to␈αmeet␈α
its␈αspecification␈αand␈α
this␈αproof␈α
has␈αbeen␈α
checked␈αby␈α
a␈αproof-checker
␈↓ ↓H␈↓program.␈α∞ In␈α
particular,␈α∞a␈α
university␈α∞level␈α∞course␈α
in␈α∞programming␈α
for␈α∞computer␈α∞science␈α
students
␈↓ ↓H␈↓should teach them to prove correct the programs they write in the course.
␈↓ ↓H␈↓ This␈α
book␈α
includes␈α
techniques␈α
for␈αproving␈α
the␈α
extensional␈α
correctness␈α
of␈αrecursive␈α
programs
␈↓ ↓H␈↓in␈αpure␈αLISP,␈αand␈αmost␈αof␈αthe␈αprogramming␈αexercises␈αare␈αwithin␈αeasy␈αrange␈αof␈α
these␈αtechniques.
␈↓ ↓H␈↓Thus␈α∃recent␈α∃Stanford␈α∃examinations␈α∃have␈α∃included␈α∃programming␈α∃problems␈α∃together␈α∃with␈α∃a
␈↓ ↓H␈↓requirement␈α∩that␈α⊃the␈α∩solution␈α⊃be␈α∩proved␈α⊃to␈α∩meet␈α⊃certain␈α∩specifications.␈α⊃ Some␈α∩techniques␈α⊃are
␈↓ ↓H␈↓available␈αfor␈α
non-extensional␈αproperties␈αsuch␈α
as␈αthe␈α
number␈αof␈αoperations␈α
executed,␈αand␈αsome␈α
are
␈↓ ↓H␈↓available for sequential programs.
␈↓ ↓H␈↓ Recursive␈αprograms␈αare␈αrepresented␈αas␈αfunctions␈αin␈αa␈αfirst␈αorder␈αtheory␈αand␈αare␈αdefined␈αby
␈↓ ↓H␈↓a␈α␈↓↓functional␈αequation␈↓␈αwhich␈αis␈αessentially␈αa␈αcopy␈αof␈αthe␈αrecursive␈αdefinition␈αand␈αby␈αa␈α␈↓↓minimization
␈↓ ↓H␈↓↓schema␈↓␈αwhich␈αis␈αa␈αsentence␈αschema␈αof␈αfirst␈αorder␈αlogic␈αwith␈αa␈αfree␈αfunction␈αparameter.␈α When␈αthe
␈↓ ↓H␈↓function␈α
is␈α
total␈αthe␈α
schema␈α
can␈α
be␈αdispensed␈α
with.␈α
The␈αextensional␈α
properties␈α
proved␈α
are␈αthen
␈↓ ↓H␈↓just␈α∪sentences␈α∪in␈α∪first␈α∀order␈α∪logic,␈α∪and␈α∪its␈α∪familiar␈α∀apparatus␈α∪is␈α∪available␈α∪for␈α∀proofs.␈α∪ The
␈↓ ↓H␈↓technique␈α∩is␈α∩much␈α∩more␈α∩direct␈α∩and␈α∩easier␈α∩to␈α∩use␈α∩than␈α∩the␈α∩methods␈α∩available␈α∩for␈α⊃sequential
␈↓ ↓H␈↓programs such as ␈↓↓inductive assertions␈↓ and the Hoare axiomatization.
␈↓ ↓H␈↓ In␈αfact␈αthe␈αproofs␈αare␈αreadily␈αcomputer-checked␈αby␈αRichard␈αWeyhrauch's␈αfirst␈αorder␈αproof-
␈↓ ↓H␈↓checker␈α∂FOL.␈α∞ However,␈α∂FOL␈α∞has␈α∂many␈α∂conventions␈α∞and␈α∂runs␈α∞rather␈α∂slowly,␈α∞so␈α∂we␈α∂didn't␈α∞feel
␈↓ ↓H␈↓ii␈↓ ¬FINTRODUCTION␈↓ H
␈↓ ↓H␈↓justified␈αin␈αrequiring␈αcomputer␈αchecking␈αof␈αthe␈αproofs␈αin␈αthe␈αcourse␈αon␈αwhich␈αthis␈αbook␈αis␈αbased.
␈↓ ↓H␈↓We␈α∂may␈α∂be␈α⊂able␈α∂to␈α∂include␈α∂computer␈α⊂proof-checking␈α∂in␈α∂a␈α∂future␈α⊂version␈α∂of␈α∂the␈α∂course␈α⊂and␈α∂a
␈↓ ↓H␈↓future edition of the book.
␈↓ ↓H␈↓ Additional␈αtheoretical␈αtopics␈αinclude␈αthe␈αuniversal␈αLISP␈αfunction,␈α␈↓↓eval␈↓␈αwhich␈αalso␈αserves␈αas
␈↓ ↓H␈↓a␈αLISP␈αinterpreter,␈α␈↓↓abstract␈αsyntax␈↓␈αwhich␈αenables␈αconvenient␈αproofs␈αof␈αthe␈αcorrectness␈αof␈αa␈αsimple
␈↓ ↓H␈↓compiler, and a slight discussion of computability.
␈↓ ↓H␈↓ An additional applied topic is syntax directed computation.
␈↓ ↓H␈↓␈↓ εH␈↓ 91
␈↓ ↓H␈↓α␈↓ επChapter I
␈↓ ↓H␈↓α␈↓ ¬εINTRODUCTION TO LISP
␈↓ ↓H␈↓ LISP␈α⊂is␈α⊂a␈α∂language␈α⊂for␈α⊂writing␈α∂programs␈α⊂that␈α⊂do␈α∂symbolic␈α⊂computation.␈α⊂ Information␈α∂is
␈↓ ↓H␈↓coded␈α∩or␈α∩represented␈α∩as␈α∩S-expressions.␈α∩ A␈α∩LISP␈α∩program␈α∩can␈α∩also␈α∩be␈α∩represented␈α∩as␈α∩an␈α∩S-
␈↓ ↓H␈↓expression.␈α∞ This␈α∂gives␈α∞LISP␈α∞the␈α∂special␈α∞ability␈α∞to␈α∂easily␈α∞manipulate␈α∞programs␈α∂as␈α∞well␈α∂as␈α∞other
␈↓ ↓H␈↓sorts␈α∞of␈α∞symbolic␈α∂data.␈α∞ In␈α∞this␈α∂chapter␈α∞we␈α∞describe␈α∂S-expressions,␈α∞show␈α∞how␈α∂S-expressions␈α∞are
␈↓ ↓H␈↓represented␈α⊂in␈α⊃a␈α⊂computer␈α⊂and␈α⊃give␈α⊂the␈α⊂basic␈α⊃functions␈α⊂and␈α⊂predicates␈α⊃on␈α⊂the␈α⊂domain␈α⊃of␈α⊂S-
␈↓ ↓H␈↓expressions.␈α We␈αthen␈αdescribe␈αthe␈αbasic␈αconstructs␈αof␈αLISP␈αand␈αshow␈αhow␈αthey␈αare␈αused␈αto␈αform
␈↓ ↓H␈↓LISP programs.
␈↓ ↓H␈↓1. ␈↓αLists.␈↓
␈↓ ↓H␈↓ Before␈αgiving␈αa␈α
formal␈αdefinition␈αof␈α
S-expressions,␈αwe␈αshall␈α
give␈αsome␈αexamples.␈α The␈α
most
␈↓ ↓H␈↓common␈α∂form␈α∂of␈α∂S-expression␈α∂is␈α∂the␈α∂list,␈α∂and␈α∂figure␈α∂1␈α∂shows␈α∂some␈α∂lists.␈α∂ The␈α∂list␈α∂(i)␈α∂has␈α∂four
␈↓ ↓H␈↓elements.␈α
The␈α
list␈α(ii)␈α
has␈α
four␈αelements␈α
one␈α
of␈αwhich␈α
is␈α
itself␈αa␈α
list.␈α
The␈αlist␈α
(iii)␈α
has␈αone␈α
element.
␈↓ ↓H␈↓The␈α∞list␈α∞(iv)␈α∞also␈α∞has␈α∞one␈α∞element␈α∞which␈α∞itself␈α∞is␈α∞a␈α∞list.␈α∞ The␈α∞list␈α∞(v)␈α∞has␈α∞no␈α∞elements;␈α∞it␈α∞is␈α∞also
␈↓ ↓H␈↓written ␈↓¬NIL␈↓.
␈↓ ↓H␈↓␈↓ ¬_(i)␈↓ ε8␈↓¬(A B C E) ␈↓
␈↓ ↓H␈↓␈↓ ¬_(ii)␈↓ ε8␈↓¬(A B (C D) E)␈↓
␈↓ ↓H␈↓␈↓ ¬_(iii)␈↓ ε8␈↓¬(A) ␈↓
␈↓ ↓H␈↓␈↓ ¬_(iv)␈↓ ε8␈↓¬((A B C D)) ␈↓
␈↓ ↓H␈↓␈↓ ¬_(v)␈↓ ε8␈↓¬() ␈↓
␈↓ ↓H␈↓␈↓ ¬h␈↓Figure 1.␈↓
␈↓ ↓H␈↓ Figure␈α∩2␈α∪shows␈α∩how␈α∩symbolic␈α∪information␈α∩can␈α∩be␈α∪represented␈α∩by␈α∩lists.␈α∪ Each␈α∩example
␈↓ ↓H␈↓consists␈αof␈αa␈α
list␈αand␈αa␈α
"mathematical"␈αrepresentation␈αof␈α
the␈αsame␈αinformation.␈α
Examples␈α(i)-(iv)
␈↓ ↓H␈↓represent␈α⊂familiar␈α⊂mathematical␈α⊂and␈α⊃logical␈α⊂expressions.␈α⊂ The␈α⊂list␈α⊃(v)␈α⊂is␈α⊂used␈α⊂to␈α⊃represent␈α⊂the
␈↓ ↓H␈↓network␈α∩or␈α∩graph␈α∩shown␈α∩according␈α∩to␈α∩a␈α∩scheme␈α∩whereby␈α∩there␈α∩is␈α∩a␈α∩sublist␈α∩for␈α∩each␈α∩vertex
␈↓ ↓H␈↓consisting of the vertex itself followed by the vertices to which it is connected.
␈↓ ↓H␈↓ The␈αelements␈αof␈αa␈αlist␈αare␈αsurrounded␈αby␈αparentheses␈αand␈αseparated␈αby␈αspaces.␈α A␈α
list␈αmay
␈↓ ↓H␈↓have␈α
any␈αnumber␈α
of␈α
terms␈αand␈α
any␈αof␈α
these␈α
terms␈αmay␈α
themselves␈αbe␈α
lists.␈α
In␈αthis␈α
case,␈αthe␈α
spaces
␈↓ ↓H␈↓surrounding␈α∞a␈α∞sublist␈α∞may␈α∂be␈α∞omitted,␈α∞and␈α∞extra␈α∞spaces␈α∂between␈α∞elements␈α∞of␈α∞a␈α∞list␈α∂are␈α∞allowed.
␈↓ ↓H␈↓Thus the lists ␈↓¬(A B(C D) E)␈↓ and ␈↓¬(A B (C D) E)␈↓ are regarded as the same.
␈↓ ↓H␈↓2␈↓ εβChapter I␈↓ H
␈↓ ↓H␈↓␈↓ αX(i)␈↓ βx␈↓¬(PLUS X Y)␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓x + y␈↓
␈↓ ↓H␈↓␈↓ αX(ii)␈↓ βx␈↓¬(PLUS (TIMES X Y) X 3)␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓xy + x + 3␈↓.
␈↓ ↓H␈↓␈↓ αX(iii)␈↓ βx␈↓¬(EXIST X (ALL Y (IMPLIES (P X) (P Y))))␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓∃x: ∀y: [P(x)⊃P(y)]␈↓.
␈↓ ↓H␈↓␈↓ αX(iv)␈↓ βx␈↓¬(INTEGRAL 0 ∞ (TIMES (EXP (TIMES I X Y)) (F X)) X)␈↓
␈↓ ↓H␈↓␈↓ βx␈↓π&␈↓β0␈↓∧␈↓#
∞␈↓#␈↓↓e␈↓∧ixy␈↓↓f(x)dx␈↓.
␈↓ ↓H␈↓␈↓ αX(v)␈↓ βx␈↓¬((A B) (B A C D) (C B D E) (D B C E) (E C D F) (F E))␈↓
␈↓"␈↓ ↓H␈↓␈↓ ε8C
␈↓"␈↓ ↓H␈↓␈↓ ε_≤'~`≥
␈↓"␈↓ ↓H␈↓␈↓ ¬x≤' ~ `≥
␈↓"␈↓ ↓H␈↓␈↓ ¬8B ≤' ~ `≥ E
␈↓"␈↓ ↓H␈↓␈↓ ∧(A αααααααα␈↓ ¬H'␈↓ ¬H≥␈↓ ε8~␈↓ π(≤␈↓ π(`αααααααα F
␈↓"␈↓ ↓H␈↓␈↓ ¬X`≥ ~ ≤'
␈↓"␈↓ ↓H␈↓␈↓ ¬x`≥ ~ ≤'
␈↓"␈↓ ↓H␈↓␈↓ ε_`≥~≤'
␈↓"␈↓ ↓H␈↓␈↓ ε8D
␈↓ ↓H␈↓␈↓ ελ␈↓Figure 2.␈↓
␈↓ ↓H␈↓2. ␈↓αAtoms.␈↓
␈↓ ↓H␈↓ The␈α
expressions␈α∞␈↓¬A,␈α
B,␈α∞X,␈α
Y,␈α
3,␈α∞PLUS␈↓,␈α
and␈α∞␈↓¬ALL␈↓␈α
occurring␈α∞in␈α
the␈α
lists␈α∞in␈α
figures␈α∞1␈α
and␈α∞2␈α
are
␈↓ ↓H␈↓called␈αatoms.␈α In␈αgeneral,␈αan␈αatom␈αis␈αexpressed␈αby␈αa␈αsequence␈αof␈αcapital␈αletters,␈αdigits,␈αand␈αspecial
␈↓ ↓H␈↓characters␈α
with␈α
certain␈α
exclusions.␈α
The␈α
exclusions␈αare␈α
<space>,␈α
<carriage␈α
return>,␈α
and␈α
the␈αother
␈↓ ↓H␈↓non-printing␈αcharacters,␈α
and␈αalso␈α
the␈αparentheses,␈α
brackets,␈αsemi-colon,␈α
and␈αcomma.␈α Numbers␈α
are
␈↓ ↓H␈↓expressed␈α_as␈α→signed␈α_decimal␈α→or␈α_octal␈α_numbers,␈α→the␈α_exact␈α→convention␈α_depending␈α→on␈α_the
␈↓ ↓H␈↓implementation.␈α Floating␈α
point␈αnumbers␈α
are␈αwritten␈α
with␈αdecimal␈α
points␈αand,␈α
when␈αappropriate,
␈↓ ↓H␈↓an␈α↔exponent␈α↔notation␈α↔depending␈α↔on␈α⊗the␈α↔implementation.␈α↔ The␈α↔reader␈α↔should␈α↔consult␈α⊗the
␈↓ ↓H␈↓programmer's manual for the LISP implementation he intends to use.
␈↓ ↓H␈↓ Some further examples of atoms are
␈↓ ↓H␈↓␈↓ εβChapter I␈↓ 93
␈↓ ↓H␈↓␈↓ ¬X␈↓¬THE-LAST-TRUMP␈↓
␈↓ ↓H␈↓␈↓ ¬q␈↓¬A307B ␈↓
␈↓ ↓H␈↓␈↓ ¬y␈↓¬345 ␈↓
␈↓ ↓H␈↓␈↓ ¬t␈↓¬3.14159, ␈↓
␈↓ ↓H␈↓and from these we can form lists like ␈↓¬((345 3.14159 -47) A307B THE-LAST-TRUMP -45.21)␈↓.
␈↓ ↓H␈↓3. ␈↓αList structures.␈↓
␈↓ ↓H␈↓ Lists␈αare␈αrepresented␈αin␈αthe␈αmemory␈αof␈αthe␈αcomputer␈αby␈αlist␈αstructures.␈α A␈αlist␈αstructure␈αis␈αa
␈↓ ↓H␈↓collection␈αof␈αmemory␈α
words␈αeach␈αof␈α
which␈αis␈αdivided␈αinto␈α
two␈αparts,␈αand␈α
each␈αpart␈αis␈α
capable␈αof
␈↓ ↓H␈↓containing␈αan␈αaddress␈αin␈αmemory.␈αThe␈αtwo␈αparts␈αare␈αcalled␈αare␈αcalled␈αthe␈αa-part␈αand␈α
the␈αd-part.
␈↓ ↓H␈↓There␈α
is␈α∞one␈α
computer␈α∞word␈α
for␈α
each␈α∞element␈α
of␈α∞the␈α
list.␈α
The␈α∞a-part␈α
of␈α∞the␈α
word␈α∞contains␈α
the
␈↓ ↓H␈↓address␈α∞of␈α
the␈α∞list␈α
or␈α∞atom␈α
representing␈α∞the␈α
element␈α∞and␈α
the␈α∞d-part␈α
contains␈α∞the␈α
address␈α∞of␈α
the
␈↓ ↓H␈↓word␈α
representing␈α
the␈αnext␈α
element␈α
of␈α
the␈αlist.␈α
If␈α
the␈α
list␈αelement␈α
is␈α
itself␈α
a␈αlist,␈α
then,␈α
of␈αcourse,␈α
the
␈↓ ↓H␈↓address␈αof␈αthe␈αfirst␈αword␈αof␈αits␈αlist␈α
structure␈αis␈αgiven␈αin␈αthe␈αa-part␈αof␈αthe␈αword␈α
representing␈αthat
␈↓ ↓H␈↓element.␈α
A␈α
diagram␈α
shows␈α
this␈α
more␈α
clearly␈α
than␈α
words,␈α
and␈α
the␈α
list␈α
structure␈α
corresponding␈α
to
␈↓ ↓H␈↓the␈α⊃list␈α⊃ ␈↓¬(PLUS (TIMES X Y) X 3)␈↓ which␈α⊃may␈α⊃represent␈α⊃the␈α⊃expression␈α⊃ ␈↓↓xy + x + 3␈↓ is␈α⊃shown␈α⊃in
␈↓ ↓H␈↓figure 3.␈α
A␈α
partial␈αdump␈α
of␈α
the␈αmemory␈α
of␈α
a␈αcomputer␈α
containing␈α
this␈αlist␈α
structure␈α
might␈αlook
␈↓ ↓H␈↓as shown in figure 4.
␈↓"
␈↓"
␈↓"␈↓ ↓H␈↓ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ααααα→~ ~ εαααα→~ ~ εαααα→~ ~ εαααα→~ ~ εααααααα⊃
␈↓"␈↓ ↓H␈↓ %απα∀ααα$ %απα∀ααα$ %απα∀ααα$ %απα∀ααα$ ~
␈↓"␈↓ ↓H␈↓ ↓ ~ ~ ↓ ~
␈↓"␈↓ ↓H␈↓ PLUS ~ ~ 3 ~
␈↓"␈↓ ↓H␈↓ ~ ⊂αααπααα⊃ ~ ⊂αααπααα⊃ ⊂αααπααα⊃ ~
␈↓"␈↓ ↓H␈↓ %α→~ ~ εααβα→~ ~ εαααα→~ ~ ~ ~
␈↓"␈↓ ↓H␈↓ %απα∀ααα$ ~ %απα∀ααα$ %απα∀απα$ ~
␈↓"␈↓ ↓H␈↓ ↓ ~ ~ ↓ ~ ~
␈↓"␈↓ ↓H␈↓ TIMES %απαα$ Y %ααπα$
␈↓"␈↓ ↓H␈↓ ↓ ↓
␈↓"␈↓ ↓H␈↓ X NIL
␈↓"
␈↓ ↓H␈↓␈↓ ε␈↓Figure 3.␈↓
␈↓ ↓H␈↓ Atoms␈αare␈αrepresented␈αby␈αthe␈αaddresses␈αof␈αtheir␈αproperty␈αlists␈αwhich␈αare␈αlist␈αstructures␈αof␈αa
␈↓ ↓H␈↓special␈αkind␈αdepending␈α
on␈αthe␈αimplementation.␈α
Thus␈α␈↓X␈↓␈αdenotes␈α
the␈αaddress␈αof␈α
the␈αproperty␈αlist␈α
of
␈↓ ↓H␈↓the␈αatom␈αnamed␈α␈↓¬X␈α␈↓and␈α␈↓/3␈↓␈αdenotes␈αthat␈αof␈αthe␈αatom␈αnamed␈α␈↓¬3␈↓.␈α (In␈αsome␈αimplementations,␈αthe␈αfirst
␈↓ ↓H␈↓word␈αof␈αa␈αproperty␈αlist␈αis␈αin␈αa␈αspecial␈αarea␈αof␈αmemory,␈αin␈αothers␈αthe␈αfirst␈αword␈αis␈αdistinguished␈αby
␈↓ ↓H␈↓sign,␈α
in␈α∞still␈α
others␈α∞it␈α
has␈α∞a␈α
special␈α∞a-part.␈α
For␈α∞basic␈α
LISP␈α∞programming,␈α
it␈α∞is␈α
enough␈α∞to␈α
know
␈↓ ↓H␈↓that␈αatoms␈αare␈αdistinguishable␈αfrom␈αother␈αlist␈αstructures␈αby␈αa␈αpredicate␈αcalled␈α␈↓αat␈↓.)␈αNotice␈α
that␈αthe
␈↓ ↓H␈↓addresses␈αof␈αconsecutive␈αlist␈αelements␈αneed␈αnot␈αbe␈αconsecutive.␈α Also␈αthe␈αlast␈αword␈αof␈αa␈αlist␈αcannot
␈↓ ↓H␈↓have␈α
the␈α
address␈α
of␈αa␈α
next␈α
word␈α
in␈α
its␈αd-part␈α
since␈α
there␈α
isn't␈α
any␈αnext␈α
word,␈α
so␈α
it␈α
has␈αthe␈α
address
␈↓ ↓H␈↓of a special atom called ␈↓¬NIL␈↓.
␈↓ ↓H␈↓4␈↓ εβChapter I␈↓ H
␈↓ ↓H␈↓ address a-part d-part address a-part d-part
␈↓ ↓H␈↓ _______ ______ ______ _______ ______ ______
␈↓ ↓H␈↓ 86 TIMES 87 1000 PLUS 1001
␈↓ ↓H␈↓ 87 X 88 1001 86 1002
␈↓ ↓H␈↓ 88 Y NIL 1002 X 2653
␈↓ ↓H␈↓ 2653 /3 NIL
␈↓ ↓H␈↓␈↓ εFigure 4.
␈↓ ↓H␈↓ A␈αprogram␈αrefers␈αto␈αa␈αlist␈αby␈αthe␈αaddress␈αof␈αits␈αfirst␈αelement.␈α According␈αto␈αthis␈αconvention,
␈↓ ↓H␈↓we␈α
see␈α
that␈α
the␈α∞a-part␈α
of␈α
a␈α
list␈α∞word␈α
is␈α
the␈α
list␈α∞element␈α
and␈α
the␈α
d-part␈α∞is␈α
a␈α
pointer␈α
to␈α∞a␈α
sublist
␈↓ ↓H␈↓formed␈αby␈αdeleting␈αthe␈αfirst␈αelement.␈α Thus␈αthe␈αfirst␈αword␈αof␈αthe␈αlist␈αstructure␈αof␈αfigure 3␈αcontains
␈↓ ↓H␈↓a␈α⊂pointer␈α⊂to␈α⊂the␈α⊂list␈α⊃structure␈α⊂representing␈α⊂the␈α⊂atom␈α⊂␈↓¬PLUS␈↓,␈α⊃ while␈α⊂its␈α⊂d-part␈α⊂points␈α⊂to␈α⊃the␈α⊂list
␈↓ ↓H␈↓␈↓¬((TIMES X Y) X 3)␈↓.␈α∞ The␈α∞second␈α∞word␈α∞contains␈α∞the␈α
list␈α∞structure␈α∞representing␈α∞␈↓¬(TIMES X Y)␈↓␈α∞in␈α∞its␈α
a-
␈↓ ↓H␈↓part␈αand␈αthe␈αlist␈αstructure␈αrepresenting␈α␈↓¬(X 3)␈↓␈αin␈αits␈αd-part.␈α The␈αlast␈αword␈αpoints␈αto␈αthe␈αatom␈α␈↓¬3␈↓␈αin
␈↓ ↓H␈↓its␈αa-part␈αand␈αhas␈αa␈αpointer␈αto␈αthe␈αatom␈α␈↓¬NIL␈↓␈αin␈αits␈αd-part.␈α This␈αis␈αconsistent␈αwith␈αthe␈αconvention
␈↓ ↓H␈↓that ␈↓¬NIL␈↓ represents the null list.
␈↓ ↓H␈↓4. ␈↓αS-expressions.␈↓
␈↓ ↓H␈↓ When␈α∪we␈α∪examine␈α∩the␈α∪way␈α∪list␈α∩structures␈α∪represent␈α∪lists␈α∩we␈α∪see␈α∪a␈α∪curious␈α∩asymmetry.
␈↓ ↓H␈↓Namely,␈αthe␈αa-part␈αof␈αa␈αlist␈αword␈αcan␈αcontain␈αan␈αatom␈αor␈αa␈αlist,␈αbut␈αthe␈αd-part␈αcan␈αcontain␈αonly␈α
a
␈↓ ↓H␈↓list␈αor␈αthe␈αspecial␈αatom␈α␈↓¬NIL␈↓.␈α This␈αrestriction␈αis␈αquite␈αunnatural␈αfrom␈αthe␈αcomputing␈αpoint␈αof␈αview,
␈↓ ↓H␈↓and␈αwe␈αshall␈α
allow␈αarbitrary␈αatoms␈α
to␈αinhabit␈αthe␈α
d-parts␈αof␈αwords,␈α
but␈αthen␈αwe␈α
must␈αgeneralize
␈↓ ↓H␈↓the␈α
way␈α
list␈αstructures␈α
are␈α
expressed␈αas␈α
character␈α
strings.␈α
To␈αdo␈α
this,␈α
we␈αintroduce␈α
the␈α
notion␈αof␈α
S-
␈↓ ↓H␈↓expression.
␈↓ ↓H␈↓ An␈α∩S-expression␈α∩is␈α∪either␈α∩an␈α∩atom␈α∪or␈α∩a␈α∩pair␈α∩of␈α∪S-expressions␈α∩separated␈α∩by␈α∪" . "␈α∩and
␈↓ ↓H␈↓surrounded by parentheses. In BNF, we can write
␈↓ ↓H␈↓␈↓ β <S-expression> ::= <atom> | (<S-expression> . <S-expression>).
␈↓ ↓H␈↓Some examples of S-expressions are
␈↓ ↓H␈↓␈↓ ¬G␈↓¬A ␈↓
␈↓ ↓H␈↓␈↓ ¬F␈↓¬(A . B) ␈↓
␈↓ ↓H␈↓␈↓ ¬E␈↓¬(A . (B . A)) ␈↓
␈↓ ↓H␈↓␈↓ ¬D␈↓¬(PLUS . (X . (Y . NIL)))␈↓
␈↓ ↓H␈↓␈↓ ¬G␈↓¬(3 . 3.4) ␈↓
␈↓ ↓H␈↓The␈α
spaces␈αaround␈α
the␈α" . "␈α
may␈αbe␈α
omitted␈α
when␈αthis␈α
will␈αnot␈α
cause␈αconfusion.␈α
The␈αonly␈α
possible
␈↓ ↓H␈↓confusion␈αis␈αof␈αthe␈αdot␈αseparator␈αwith␈αa␈αdecimal␈αpoint␈αin␈αnumbers.␈α Thus,␈αin␈αthe␈αabove␈αcases,␈αwe
␈↓ ↓H␈↓may write ␈↓¬(A.B)␈↓, ␈↓¬(A.(B.A))␈↓, and ␈↓¬(PLUS.(X.(Y.NIL)))␈↓, but if we wrote ␈↓¬(3.3.4)␈↓ confusion would result.
␈↓ ↓H␈↓␈↓ εβChapter I␈↓ 95
␈↓ ↓H␈↓ In␈α∂the␈α∂memory␈α∂of␈α∂a␈α∂computer,␈α∂an␈α∂S-expression␈α∂is␈α∂represented␈α∂by␈α∂the␈α∂address␈α∂of␈α⊂a␈α∂word
␈↓ ↓H␈↓whose␈αa-part␈αcontains␈αthe␈αfirst␈αelement␈αof␈αthe␈αpair␈αand␈αwhose␈αd-part␈αcontains␈αthe␈αsecond␈αelement
␈↓ ↓H␈↓of␈α
the␈α
pair.␈α
Thus,␈α
the␈α
S-expressions␈α
␈↓¬(A.B)␈↓,␈α
␈↓¬(A.(B.A))␈↓,␈α
and␈α
␈↓¬(PLUS.(X.(Y.NIL)))␈↓␈α
are␈α
represented␈α∞by␈α
the
␈↓ ↓H␈↓list structures of figure 5.
␈↓"
␈↓"
␈↓"␈↓ ↓H␈↓ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ αααα→~ ~ ~ αααα→~ ~ εαααα→~ ~ ~
␈↓"␈↓ ↓H␈↓ %απα∀απα$ %απα∀ααα$ %απα∀απα$
␈↓"␈↓ ↓H␈↓ ↓ ↓ ~ ↓ ~
␈↓"␈↓ ↓H␈↓ A B ~ B ~
␈↓"␈↓ ↓H␈↓ ~ ~
␈↓"␈↓ ↓H␈↓ ~ ~
␈↓"␈↓ ↓H␈↓ %ααααααααπαααααααα$
␈↓"␈↓ ↓H␈↓ ↓
␈↓"␈↓ ↓H␈↓ A
␈↓"
␈↓"␈↓ ↓H␈↓ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ αααα→~ ~ εαααα→~ ~ εαααα→~ ~ ~
␈↓"␈↓ ↓H␈↓ %απα∀ααα$ %απα∀ααα$ %απα∀απα$
␈↓"␈↓ ↓H␈↓ ↓ ↓ ↓ ↓
␈↓"␈↓ ↓H␈↓ PLUS X Y NIL
␈↓"
␈↓"
␈↓ ↓H␈↓␈↓ ε␈↓Figure 5.␈↓
␈↓ ↓H␈↓ Note␈αthat␈α
the␈αlist␈α␈↓¬(PLUS X Y)␈↓␈α
and␈αthe␈αS-expression␈α
␈↓¬(PLUS . (X . (Y . NIL)))␈↓␈αare␈α
represented␈αin
␈↓ ↓H␈↓memory␈α
by␈α
the␈α∞same␈α
list␈α
structure.␈α∞ The␈α
simplest␈α
way␈α∞to␈α
treat␈α
this␈α∞is␈α
to␈α
regard␈α∞S-expressions␈α
as
␈↓ ↓H␈↓primary␈α∞and␈α∞lists␈α∞as␈α∞abbreviations␈α∞for␈α∞certain␈α∞S-expressions,␈α∞namely␈α∞those␈α∞that␈α∞never␈α∞have␈α
any
␈↓ ↓H␈↓atom␈αbut␈α␈↓¬NIL␈↓␈αas␈αthe␈αsecond␈αpart␈αof␈αa␈αpair.␈α In␈αgeneral,␈αthe␈αlist␈α␈↓¬(A B . . . Z)␈↓␈αmay␈αbe␈αregarded␈αas␈αan
␈↓ ↓H␈↓abbreviation␈α
of␈αthe␈α
S-expression␈α
␈↓¬(A . (B . (C . (... (Z . NIL) ...)))␈↓.␈α In␈α
particular␈α
the␈αa-part␈α
of␈α
a␈αlist␈α
can
␈↓ ↓H␈↓be␈αany␈αS-expression,␈αbut␈αthe␈αd-part␈αof␈αa␈αlist␈αmust␈αbe␈αa␈αlist␈αand␈αand␈αthe␈αonly␈αatomic␈αlist␈αis␈α␈↓¬NIL␈↓.␈α
In
␈↓ ↓H␈↓giving␈α∞input␈α∞to␈α∞LISP,␈α∞either␈α∞the␈α∞list␈α∞form␈α∞or␈α∞the␈α∞S-expression␈α∞form␈α∞may␈α∞be␈α∞used␈α∞for␈α∞lists.␈α
On
␈↓ ↓H␈↓output,␈α∂LISP␈α∞will␈α∂print␈α∞a␈α∂list␈α∞structure␈α∂as␈α∞a␈α∂list␈α∞as␈α∂far␈α∞as␈α∂it␈α∞can,␈α∂otherwise␈α∞as␈α∂an␈α∞S-expression.
␈↓ ↓H␈↓Thus,␈α
some␈αlist␈α
structures␈αwill␈α
be␈αprinted␈α
in␈α
a␈αmixed␈α
notation,␈αe.g.␈α
␈↓¬((A . B) (C . D) (3))␈↓.␈α Some␈α
LISPs
␈↓ ↓H␈↓use␈αand␈α"extended"␈αlist␈αnotation␈αfor␈αprinting.␈α In␈αthis␈αnotation␈αthe␈αprint␈αroutine␈α␈↓↓cdr␈↓s␈αthrough␈αthe
␈↓ ↓H␈↓S-expression␈α∞printing␈α∞the␈α∞␈↓↓car␈↓␈α∞at␈α∞each␈α∞step␈α∞as␈α∞though␈α∞it␈α∞were␈α∞printing␈α∞a␈α∞list.␈α∞ When␈α∞an␈α∞atom␈α
is
␈↓ ↓H␈↓reached␈αif␈αthe␈αatom␈αis␈α␈↓¬NIL␈↓␈αthe␈αprinting␈αis␈αterminated␈αwith␈α")"␈αas␈αusual,␈α other␈αwise␈αa␈α" . "␈αfollowed
␈↓ ↓H␈↓by the atom followed by ")" is printed. Thus ␈↓¬(A . (B . (C . D)))␈↓ would print as ␈↓¬(A B C . D)␈↓.
␈↓ ↓H␈↓α␈↓ ε
Exercises
␈↓ ↓H␈↓ 1.␈αIf␈αwe␈αrepresent␈αsums␈αand␈αproducts␈αas␈αindicated␈αabove␈αand␈αuse␈α␈↓¬(MINUS X)␈↓,␈α␈↓¬(QUOTIENT X Y)␈↓,
␈↓ ↓H␈↓and ␈↓¬(POWER X Y)␈↓ as representations of ␈↓↓-x,␈↓ ␈↓↓x/y,␈↓ and ␈↓↓x␈↓∧y␈↓ respectively, then
␈↓ ↓H␈↓ a. What do the following lists represent?
␈↓ ↓H␈↓␈↓ ∧3␈↓↓␈↓¬(QUOTIENT 2 (POWER (PLUS X (MINUS Y)) 3))␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧,␈↓↓␈↓¬(PLUS -2 (MINUS 2) (TIMES X (POWER Y 3.3)))␈↓↓␈↓
␈↓ ↓H␈↓ b. How are the expressions ␈↓↓xyz+3(u+v)␈↓∧-3␈↓ and ␈↓↓(xy-yx)/(xy+yx)␈↓ to be represented?
␈↓ ↓H␈↓6␈↓ εβChapter I␈↓ H
␈↓ ↓H␈↓ 2. In the above mentioned graph notation, what graph is represented by the list
␈↓ ↓H␈↓␈↓ βJ␈↓¬((A D E F) (B D E F) (C D E F) (D A B C) (E A B C) (F A B C))␈↓?
␈↓ ↓H␈↓ 3.␈αWrite␈αthe␈αlist␈α␈↓¬((PLUS␈α(TIMES␈αX␈αY)␈αX␈α3)␈↓␈αas␈αan␈αS-expression.␈α This␈αis␈αsometimes␈α
referred␈αto
␈↓ ↓H␈↓as "dot-notation".
␈↓ ↓H␈↓ 4. Write the following S-expressions in list notation to whatever extent is possible:
␈↓ ↓H␈↓ a. ␈↓¬(A . NIL)␈↓
␈↓ ↓H␈↓ b. ␈↓¬(A . B)␈↓
␈↓ ↓H␈↓ c. ␈↓¬((A . NIL) . B)␈↓
␈↓ ↓H␈↓ d. ␈↓¬((A . B) . ((C . D) . NIL)␈↓.
␈↓ ↓H␈↓ 5.␈α∂Consider␈α∂a␈α∂data␈α∂base␈α∂of␈α⊂directory␈α∂accounting␈α∂information␈α∂for␈α∂a␈α∂computer␈α⊂center.␈α∂ For
␈↓ ↓H␈↓each␈αdirectory␈αthe␈αfollowing␈αinformation␈αis␈αavailable:␈α the␈αaccount␈αnumber,␈αthe␈αproject␈α
name,␈αthe
␈↓ ↓H␈↓names␈α
of␈α
programmers␈α
using␈α
this␈α
directory,␈α
the␈αprotection␈α
status␈α
(a␈α
binary␈α
bit␈α
string),␈α
the␈αnames␈α
of
␈↓ ↓H␈↓all␈α∪files␈α∪on␈α∩this␈α∪directory,␈α∪and␈α∪for␈α∩each␈α∪file␈α∪the␈α∩date␈α∪it␈α∪was␈α∪last␈α∩written␈α∪and␈α∪name␈α∪of␈α∩the
␈↓ ↓H␈↓programmer␈α∪who␈α∪wrote␈α∪it.␈α∪ Represent␈α∪this␈α∩information␈α∪using␈α∪list␈α∪structures.␈α∪ Show␈α∪how␈α∩the
␈↓ ↓H␈↓information␈αcould␈αbe␈αretrieved.␈α For␈αexample␈αcould␈αyou␈αproduce␈αa␈αlist␈αof␈αall␈αfiles␈αwritten␈αlast␈αby␈αa
␈↓ ↓H␈↓given programmer using your representation of the data?
␈↓ ↓H␈↓ 6.␈αProve␈αthat␈αthe␈αnumber␈αof␈α"shapes"␈αof␈αS-expressions␈αwith␈α␈↓↓n␈↓␈αatoms␈αis␈α␈↓↓(2n␈α-␈α2)!/(n!(n␈α-␈α1)!)␈↓.
␈↓ ↓H␈↓(The two shapes of S-expressions with three atoms are ␈↓¬(A.(B.C))␈↓ and ␈↓¬((A.B).C)␈↓.
␈↓ ↓H␈↓ 7.␈αThe␈αabove␈αresult␈αcan␈αalso␈αbe␈αobtained␈αby␈αwriting␈α␈↓↓S = A + S␈↓π#␈↓↓S␈↓␈αas␈αan␈α"equation"␈αsatisfied
␈↓ ↓H␈↓by␈α∞the␈α∞set␈α∞of␈α
S-expressions␈α∞with␈α∞the␈α∞interpretation␈α
that␈α∞an␈α∞S-expression␈α∞is␈α
either␈α∞an␈α∞atom␈α∞or␈α
a
␈↓ ↓H␈↓pair␈αof␈α
S-expressions.␈α The␈αnext␈α
step␈αis␈α
to␈αregard␈αthe␈α
equation␈αas␈αthe␈α
quadratic␈α␈↓↓S␈↓∧2␈↓↓ - S + A␈α
=␈α0␈↓,
␈↓ ↓H␈↓solve␈α
it␈α
by␈α
the␈α
quadratic␈α
formula␈α
choosing␈α
the␈α
minus␈α
sign␈α
for␈α
the␈α
square␈α
root.␈α
Then␈α
the␈αradical␈α
is
␈↓ ↓H␈↓regarded␈α
as␈α
the␈α
1/2␈α
power␈α
and␈α∞expanded␈α
by␈α
the␈α
binomial␈α
theorem.␈α
Manipulating␈α∞the␈α
binomial
␈↓ ↓H␈↓coefficients␈α∞yields␈α∞the␈α∂above␈α∞formula␈α∞as␈α∞the␈α∂coefficient␈α∞of␈α∞␈↓↓A␈↓∧n␈↓␈α∞in␈α∂the␈α∞expansion.␈α∞ Why␈α∂does␈α∞this
␈↓ ↓H␈↓somewhat ill-motivated and irregular procedure give the right answer?
␈↓ ↓H␈↓ (The last two exercises are unconnected with subsequent material in this book.)
␈↓ ↓H␈↓5. ␈↓αThe basic functions and predicates of LISP.␈↓
␈↓ ↓H␈↓ A␈α∞LISP␈α∞function␈α∞or␈α∂predicate␈α∞is␈α∞a␈α∞function␈α∞or␈α∂predicate␈α∞on␈α∞the␈α∞domain␈α∂of␈α∞S-expressions
␈↓ ↓H␈↓which␈α∞is␈α∞either␈α∞one␈α∞of␈α∞the␈α∞basic␈α∞functions␈α∞or␈α∞predicates␈α∞described␈α∞in␈α∞this␈α∞section,␈α∞or␈α∂built␈α∞from
␈↓ ↓H␈↓these␈α
using␈α
methods␈αdescribed␈α
later␈α
in␈αthis␈α
chapter.␈α
This␈α
is␈αanalogous␈α
to␈α
numeric␈αfunctions␈α
which
␈↓ ↓H␈↓are␈α
based␈αon␈α
the␈αfour␈α
arithmetic␈α
operations␈αof␈α
addition␈αsubtraction,␈α
multiplication,␈α
and␈αdivision,
␈↓ ↓H␈↓and the arithmetic predicates of equality and comparison.
␈↓ ↓H␈↓ The␈α∩main␈α∩form␈α∩of␈α∩LISP␈α∩program␈α∪is␈α∩the␈α∩functional␈α∩program␈α∩which␈α∩computes␈α∪a␈α∩LISP
␈↓ ↓H␈↓function.␈α∂ We␈α∂will␈α∂say␈α∂that␈α∂a␈α∂LISP␈α∂predicate␈α∂is␈α∂computed␈α∂by␈α∂a␈α∂LISP␈α∂program␈α∂if␈α⊂the␈α∂function
␈↓ ↓H␈↓computed␈α
by␈α
that␈α
program␈α
has␈α
the␈α
atom␈α
␈↓¬T␈α
␈↓as␈α
its␈α
value␈α
for␈α
all␈α
arguments␈α
for␈α
which␈α
the␈αpredicate␈α
is
␈↓ ↓H␈↓true␈αand␈αhas␈αthe␈αatom␈α␈↓¬NIL␈↓␈αas␈αits␈αvalue␈αfor␈αall␈αarguments␈αfor␈αwhich␈αthe␈αpredicate␈αis␈αfalse.␈α Such␈αa
␈↓ ↓H␈↓␈↓ εβChapter I␈↓ 97
␈↓ ↓H␈↓function␈αis␈αcalled␈αa␈αrepresenting␈αfunction␈αfor␈αthe␈αpredicate.␈α (Most␈αLISP␈αimplementations␈α
use␈αthe
␈↓ ↓H␈↓convention␈αthat␈αany␈αS-expression␈αother␈αthan␈α␈↓¬NIL␈↓␈αrepresents␈α␈↓αtrue␈↓.)␈αLISP␈αprograms␈αare␈αconstructed
␈↓ ↓H␈↓from␈αLISP␈α␈↓↓term␈↓s␈αwhich␈αare␈αbuilt␈αup␈αfrom␈αvariables,␈αconstants␈αand␈αprograms␈αto␈αcompute␈αthe␈αbasic
␈↓ ↓H␈↓LISP␈α⊂functions␈α⊂and␈α⊂predicates.␈α⊂ The␈α⊂value␈α⊂of␈α⊂a␈α⊂term␈α⊂generally␈α⊂depends␈α⊂on␈α⊂the␈α⊂values␈α⊂of␈α∂the
␈↓ ↓H␈↓variables␈α⊃occuring␈α∩in␈α⊃it.␈α∩ The␈α⊃value␈α⊃of␈α∩the␈α⊃LISP␈α∩function␈α⊃computed␈α⊃by␈α∩a␈α⊃LISP␈α∩program␈α⊃is
␈↓ ↓H␈↓computed␈α⊃by␈α⊃evaluating␈α⊃the␈α∩corresponding␈α⊃term␈α⊃after␈α⊃giving␈α∩the␈α⊃variables␈α⊃the␈α⊃values␈α∩of␈α⊃the
␈↓ ↓H␈↓corresponding␈αarguments.␈α We␈αwill␈αuse␈αtwo␈αlanguages␈α-␈α␈↓↓external␈↓␈α␈↓↓language␈↓␈αand␈α␈↓↓internal␈↓␈α␈↓↓language.␈↓
␈↓ ↓H␈↓External␈α∞language␈α∞is␈α
used␈α∞for␈α∞expressing␈α∞LISP␈α
function␈α∞definitions␈α∞in␈α∞a␈α
form␈α∞that␈α∞is␈α∞easier␈α
for
␈↓ ↓H␈↓people␈αto␈αread␈αand␈α
write.␈α However␈αexternal␈αlanguage␈α
function␈αdescriptions␈αare␈αnot␈α
S-expressions,
␈↓ ↓H␈↓and␈α∞therefore␈α∞it␈α
is␈α∞not␈α∞easy␈α∞to␈α
write␈α∞LISP␈α∞programs␈α∞to␈α
generate,␈α∞interpret,␈α∞compile␈α∞or␈α
otherwise
␈↓ ↓H␈↓manipulate␈α
functions␈αdefined␈α
in␈α
external␈αlanguage.␈α
Internal␈αlanguage␈α
represents␈α
LISP␈αprograms
␈↓ ↓H␈↓and␈α
terms␈α
as␈α
S-expressions.␈α
These␈α
are␈αharder␈α
for␈α
a␈α
person␈α
to␈α
read␈αand␈α
write,␈α
but␈α
it␈α
is␈α
easier␈αfor␈α
a
␈↓ ↓H␈↓person␈α
to␈αwrite␈α
a␈αprogram␈α
to␈αmanipulate␈α
␈↓↓object␈↓␈α␈↓↓programs␈↓␈α
when␈αthe␈α
object␈αprograms␈α
are␈αin␈α
internal
␈↓ ↓H␈↓language.␈α∂ Besides␈α∞all␈α∂that,␈α∞most␈α∂LISP␈α∞implementations␈α∂accept␈α∞only␈α∂internal␈α∂language␈α∞programs
␈↓ ↓H␈↓rather than some form of external language.
␈↓ ↓H␈↓ In␈α∂the␈α∂external␈α∂language␈α∂S-expressions␈α∂will␈α∂stand␈α∂for␈α∂themselves␈α∂and␈α∂will␈α∂appear␈α⊂in␈α∂the
␈↓ ↓H␈↓special␈α∪font␈α∩reserved␈α∪for␈α∩S-expressions␈α∪and␈α∩used␈α∪in␈α∩the␈α∪preceeding␈α∩sections.␈α∪ Variables␈α∩and
␈↓ ↓H␈↓function␈αnames␈αwill␈αappear␈αin␈αlower␈αcase␈αitalics.␈α The␈αarguments␈αto␈αa␈αfunction␈αwill␈αbe␈αsurrounded
␈↓ ↓H␈↓by␈αbrackets␈α
as␈αin␈α
␈↓↓subst[x,␈αy,␈αz]␈↓␈α
thus␈αreserving␈α
parthentheses␈αfor␈α
S-expressions.␈α We␈αwill␈α
generally
␈↓ ↓H␈↓use␈αthe␈αnames␈α␈↓↓x,␈↓␈α␈↓↓y,␈↓␈α␈↓↓z,␈↓␈α␈↓↓x1,␈↓␈αetc.␈αfor␈αvariables␈αranging␈αover␈αS-expressions␈αand␈α␈↓↓u,␈↓␈α␈↓↓v,␈↓␈α␈↓↓w,␈↓␈α␈↓↓u1,␈↓␈αetc.
␈↓ ↓H␈↓for␈αvariables␈α
ranging␈αover␈αlists.␈α
In␈αinternal␈αlanguage␈α
a␈αvariable␈αor␈α
function␈αname␈αis␈α
denoted␈αby
␈↓ ↓H␈↓the␈α
corresponding␈α
upper␈α
case␈α
identifier␈α
(in␈αS-expression␈α
font).␈α
A␈α
certain␈α
ambiguity␈α
arises␈αwhen
␈↓ ↓H␈↓we␈α∞want␈α∞to␈α∞use␈α∞S-expression␈α∞constants␈α∞in␈α
the␈α∞internal␈α∞language.␈α∞ Namely,␈α∞the␈α∞S-expression␈α∞ ␈↓¬X␈α
␈↓
␈↓ ↓H␈↓could␈αeither␈αstand␈αfor␈αitself␈αor␈αstand␈αfor␈αa␈αvariable␈α ␈↓¬X␈α␈↓ whose␈αvalue␈αis␈αwanted.␈α More␈αgenerally␈α
an
␈↓ ↓H␈↓S-expression␈α∞could␈α∞either␈α∞stand␈α∞for␈α∞itself␈α∞as␈α∞a␈α∞constant␈α∞or␈α∞for␈α∞the␈α∞LISP␈α∞term␈α∞that␈α∞it␈α
represents.
␈↓ ↓H␈↓Internal␈α
language␈α
avoids␈α
the␈αambiguity␈α
by␈α
using␈α
␈↓¬(QUOTE ␈↓↓e␈↓¬)␈↓ ␈α
to␈αstand␈α
for␈α
the␈α
S-expression␈α ␈↓↓e,␈↓␈α
and
␈↓ ↓H␈↓unquoted S-expressions stand for the corresponding LISP term.
␈↓ ↓H␈↓ Now␈αwe␈αwill␈αdescribe␈αthe␈αthree␈αbasic␈αfunctions␈αand␈αtwo␈αbasic␈αpredicates␈αof␈αLISP␈αfirst␈αusing
␈↓ ↓H␈↓external␈α
language,␈αthen␈α
giving␈α
the␈αcorresponding␈α
internal␈αlanguage␈α
form.␈α
(The␈αinternal␈α
form␈αof␈α
a
␈↓ ↓H␈↓predicate␈αwill␈αactually␈αbe␈αa␈αprogram␈αthat␈αcomputes␈αthe␈αrepresenting␈αfunction␈αfor␈αthe␈α
predicate␈αas
␈↓ ↓H␈↓described above.)
␈↓ ↓H␈↓ First,␈αwe␈αhave␈αtwo␈αfunctions␈α␈↓αa␈↓␈αand␈α␈↓αd␈↓.␈α ␈↓αa|␈↓␈↓↓x␈↓␈αis␈αthe␈αa-part␈αof␈αthe␈αS-expression␈α␈↓↓x␈↓␈αand␈α␈↓αd|␈↓␈↓↓x␈↓␈αis␈αthe
␈↓ ↓H␈↓d-part of the S-expression ␈↓↓x.␈↓ Thus
␈↓ ↓H␈↓␈↓ ¬7␈↓↓ ␈↓αa|␈↓↓␈↓¬(A . B) = A␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ ¬6␈↓↓␈↓αa|␈↓↓␈↓¬((A . B) . A) = (A . B)␈↓↓,␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ¬7␈↓↓ ␈↓αd|␈↓↓␈↓¬(A . B) = B␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ ¬8␈↓↓␈↓αd|␈↓↓␈↓¬((A . B) . A) = A␈↓↓. ␈↓
␈↓ ↓H␈↓␈↓αa|␈↓␈↓↓x␈↓␈α
and␈α
␈↓αd|␈↓␈↓↓x␈↓␈α
are␈α
undefined␈α
in␈α
basic␈α
LISP␈α
when␈α
␈↓↓x␈↓␈α
is␈α
an␈α
atom,␈α
but␈α
they␈α
may␈α
have␈α
a␈α
meaning␈αin␈α
some
␈↓ ↓H␈↓implementations.␈α∞ The␈α∞internal␈α∞forms␈α∞of␈α∞␈↓αa|␈↓␈↓↓x␈↓␈α
and␈α∞␈↓αd|␈↓␈↓↓x␈↓␈α∞are␈α∞␈↓¬(CAR X)␈↓␈α∞and␈α∞␈↓¬(CDR X)␈↓␈α∞respectively.␈α
Note
␈↓ ↓H␈↓that␈αthe␈α
internal␈αform␈α
of␈αthe␈α
S-expression␈αconstant␈α␈↓¬(CAR␈α
X)␈↓␈αis␈α
␈↓¬(QUOTE␈α(CAR␈α
X))␈↓.␈α The␈α
names␈α␈↓¬CAR
␈↓ ↓H␈↓¬␈↓and␈α
␈↓¬CDR␈α␈↓stood␈α
for␈α"contents␈α
of␈αthe␈α
address␈αpart␈α
of␈αregister"␈α
and␈α"contents␈α
of␈αthe␈α
decrement␈αpart␈α
of
␈↓ ↓H␈↓register"␈α∂in␈α∂a␈α∂1957␈α∂precursor␈α∂of␈α∂LISP␈α∞projected␈α∂for␈α∂the␈α∂IBM␈α∂704␈α∂computer.␈α∂ The␈α∂names␈α∞have
␈↓ ↓H␈↓persisted for lack of a clearly preferable alternative.
␈↓ ↓H␈↓8␈↓ εβChapter I␈↓ H
␈↓ ↓H␈↓ Since␈α
lists␈α
are␈α∞a␈α
particular␈α
kind␈α∞of␈α
S-expression,␈α
the␈α∞meanings␈α
of␈α
␈↓αa␈↓ ␈α∞and␈α
␈↓αd␈↓ ␈α
for␈α∞lists␈α
are
␈↓ ↓H␈↓also␈α$determined␈α#by␈α$the␈α#above␈α$definitions.␈α# Thus,␈α$we␈α#have␈α$ ␈↓↓␈↓αa|␈↓↓␈↓¬(A B C D)␈↓↓ = ␈↓¬A␈↓↓␈↓ ␈α#and
␈↓ ↓H␈↓ ␈↓↓␈↓αd|␈↓↓␈↓¬(A B C D)␈↓↓ = ␈↓¬(B C D)␈↓↓,␈↓ ␈α
and␈αwe␈α
see␈α
that,␈αin␈α
general,␈α
␈↓↓␈↓αa|␈↓↓x␈↓ ␈αis␈α
the␈αfirst␈α
element␈α
of␈αthe␈α
list␈α
␈↓↓x,␈↓␈α and
␈↓ ↓H␈↓ ␈↓↓␈↓αd|␈↓↓x␈↓ ␈α
is␈α
the␈α
rest␈α
of␈αthe␈α
list,␈α
deleting␈α
that␈α
first␈αelement.␈α
Notice␈α
that␈α
for␈α
S-expressions,␈αthe␈α
definitions
␈↓ ↓H␈↓of␈α⊃ ␈↓↓␈↓αa|␈↓↓x␈↓ ␈α⊃and␈α⊃ ␈↓↓␈↓αd|␈↓↓x␈↓ ␈α⊃are␈α∩symmetrical,␈α⊃while␈α⊃for␈α⊃lists␈α⊃the␈α∩symmetry␈α⊃is␈α⊃lost.␈α⊃ This␈α⊃is␈α∩because␈α⊃the
␈↓ ↓H␈↓convention that defines lists in terms of S-expressions is unsymmetrical.
␈↓ ↓H␈↓ The␈α
third␈α
function␈α
␈↓↓cons[x,␈α
y]␈↓ ␈α
forms␈α
the␈α
S-expression␈α
whose␈α
a-part␈α
and␈α
d-part␈α
are␈α ␈↓↓x␈↓␈α
and
␈↓ ↓H␈↓ ␈↓↓y␈↓␈α respectively.␈α We␈αsee␈αthat␈αfor␈αlists,␈α ␈↓↓cons␈↓ ␈αis␈αa␈αprefixing␈αoperation.␈α Namely,␈α ␈↓↓cons[x,␈αy]␈↓ ␈α
is␈αthe
␈↓ ↓H␈↓list obtained by putting the element ␈↓↓x␈↓ onto the front of the list ␈↓↓y.␈↓ For example
␈↓ ↓H␈↓␈↓ ¬∪␈↓↓ cons[␈↓¬(A.B), A␈↓↓] = ␈↓¬((A.B).A)␈↓↓ ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ∧}␈↓↓cons[␈↓¬A, (B C D E)␈↓↓] = ␈↓¬(A B C D E)␈↓↓␈↓.
␈↓ ↓H␈↓If␈αwe␈αwant␈α ␈↓↓cons[x,␈αy]␈↓ ␈αto␈αbe␈αa␈αlist,␈αthen␈α ␈↓↓y␈↓␈α must␈αbe␈αa␈αlist␈α(possibly␈αthe␈αnull␈αlist␈α␈↓¬NIL␈↓),␈αand␈α ␈↓↓x␈↓␈α may
␈↓ ↓H␈↓be␈α⊃any␈α⊃S-expression.␈α⊃ In␈α∩the␈α⊃case␈α⊃of␈α⊃S-expressions␈α⊃in␈α∩general,␈α⊃there␈α⊃is␈α⊃no␈α⊃restriction␈α∩on␈α⊃the
␈↓ ↓H␈↓arguments␈α∞of␈α∞␈↓↓cons.␈↓␈α∞ Usually,␈α∞we␈α∞shall␈α∞write␈α∞ ␈↓↓x␈α∞.␈α∞y␈↓ ␈α∞instead␈α∞of␈α∞ ␈↓↓cons[x,␈α∞y]␈↓ ␈α∞since␈α∞this␈α∞is␈α∞briefer.
␈↓ ↓H␈↓The␈α∃internal␈α∃language␈α∃form␈α∃of␈α∃␈↓↓cons[x, y]␈↓␈α∃is␈α∃␈↓¬(CONS X Y)␈↓.␈α∃ The␈α∃name␈α∃"CONS"␈α∃comes␈α∃from
␈↓ ↓H␈↓"construct" referring to the way ␈↓↓cons[x, y]␈↓ is constructed from the free storage list.
␈↓ ↓H␈↓ The␈αfirst␈αpredicate␈αis␈α ␈↓αat␈↓.␈α ␈↓αat|␈↓␈↓↓x␈↓␈αis␈αtrue␈αif␈αthe␈αS-expression␈α ␈↓↓x␈↓␈α is␈αatomic␈αand␈αfalse␈αotherwise.
␈↓ ↓H␈↓The internal form is ␈↓¬(ATOM X)␈↓.
␈↓ ↓H␈↓ The␈α∀second␈α∀predicate␈α∀is␈α∀equality␈α∀of␈α∃atomic␈α∀symbols␈α∀written␈α∀ ␈↓↓x␈α∀␈↓αeq␈↓↓␈α∀y␈↓.␈α∀Equality␈α∃of␈α∀S-
␈↓ ↓H␈↓expressions␈α⊂is␈α∂tested␈α⊂by␈α⊂a␈α∂program␈α⊂based␈α∂on␈α⊂␈↓αeq␈↓.␈α⊂ The␈α∂internal␈α⊂form␈α∂is␈α⊂␈↓¬(EQ X Y)␈↓.␈α⊂ Actually␈α∂the
␈↓ ↓H␈↓program␈αcomputing␈α␈↓αeq␈↓␈αdoes␈αa␈αbit␈αmore␈αthan␈αtesting␈αequality␈αof␈αatoms.␈α Namely,␈α ␈↓↓x␈α␈↓αeq␈↓↓␈αy␈↓ ␈αis␈αtrue␈αif
␈↓ ↓H␈↓and␈αonly␈αif␈αthe␈αaddresses␈αof␈αthe␈αfirst␈αwords␈αof␈αthe␈αlist␈αstructures␈α ␈↓↓x␈↓␈α and␈α ␈↓↓y␈↓␈α are␈αequal.␈α Therefore,
␈↓ ↓H␈↓if␈α ␈↓↓x␈α␈↓αeq␈↓↓␈αy␈↓, ␈αthen␈α ␈↓↓x␈↓␈α and␈α ␈↓↓y␈↓␈α are␈αcertainly␈αthe␈αsame␈αS-expression␈αsince␈αthey␈αare␈αrepresented␈αby␈αthe
␈↓ ↓H␈↓same␈αstructure␈α
in␈αmemory.␈α
The␈αconverse␈α
is␈αfalse␈α
because␈αthere␈α
is␈αno␈α
guarantee␈αin␈α
general␈αthat␈α
the
␈↓ ↓H␈↓same␈α
S-expression␈α
is␈αnot␈α
represented␈α
by␈αtwo␈α
different␈α
list␈αstructures.␈α
However,␈α
in␈α
the␈αparticular
␈↓ ↓H␈↓case␈αwhere␈αat␈αleast␈αone␈αof␈αthe␈αS-expressions␈αis␈αknown␈αto␈αbe␈αan␈αatom,␈αthis␈αguarantee␈αcan␈αbe␈αgiven,
␈↓ ↓H␈↓because LISP represents atoms uniquely in memory.
␈↓ ↓H␈↓ The␈αabove␈αare␈α
all␈αthe␈αbasic␈αfunctions␈α
of␈αLISP;␈αall␈αother␈α
LISP␈αfunctions␈αcan␈αbe␈α
built␈αfrom
␈↓ ↓H␈↓them␈α∂using␈α∂recursive␈α∂conditional␈α∞expressions␈α∂as␈α∂will␈α∂shortly␈α∞be␈α∂explained.␈α∂However,␈α∂the␈α∂use␈α∞of
␈↓ ↓H␈↓certain abbreviations makes LISP programs easier to write and understand.
␈↓ ↓H␈↓ ␈↓αn|␈↓␈↓↓x␈↓␈α is␈α
an␈αabbreviation␈αfor␈α
␈↓↓x␈α␈↓αeq␈↓↓␈α␈↓¬NIL␈↓↓␈↓.␈α
It␈αrates␈αa␈α
special␈αnotation␈αbecause␈α
␈↓¬NIL␈↓ ␈αplays␈αthe␈α
same
␈↓ ↓H␈↓role␈α
among␈αlists␈α
that␈αzero␈α
plays␈αamong␈α
numbers,␈αand␈α
list␈α
variables␈αoften␈α
have␈αto␈α
be␈αtested␈α
to␈αsee␈α
if
␈↓ ↓H␈↓their value is ␈↓¬NIL␈↓. Its internal form is ␈↓¬(NULL X)␈↓.
␈↓ ↓H␈↓ The␈αnotation␈α ␈↓↓list[x1, x2, . . . ,xn]␈↓ ␈αis␈αused␈αto␈αdenote␈αthe␈αcomposition␈αof␈α␈↓↓cons␈↓'s␈αthat␈αforms␈αa
␈↓ ↓H␈↓list␈αfrom␈αits␈αelements.␈α Thus␈α ␈↓↓list[x, y, z]␈↓ ␈αdenotes␈α ␈↓↓cons[x, cons[y, cons[z, ␈↓¬NIL␈↓↓]]]␈↓ ␈αand␈αforms␈αa␈α
list
␈↓ ↓H␈↓of␈α∞three␈α∞elements␈α∂out␈α∞of␈α∞the␈α∂quantities␈α∞␈↓↓x,␈↓␈α∞␈↓↓y,␈↓␈α∂and␈α∞␈↓↓z.␈↓␈α∞ We␈α∂often␈α∞write␈α∞ ␈↓↓<x y . . . z>␈↓ ␈α∂instead␈α∞of
␈↓ ↓H␈↓ ␈↓↓list[x, y, . . . , z]␈↓ ␈α∃when␈α∀this␈α∃will␈α∃not␈α∀cause␈α∃confusion.␈α∀ The␈α∃experienced␈α∃implementer␈α∀of
␈↓ ↓H␈↓programming␈α∩languages␈α∪will␈α∩expect␈α∪that␈α∩since␈α∪␈↓↓list␈↓␈α∩has␈α∪a␈α∩variable␈α∪number␈α∩of␈α∪arguments,␈α∩its
␈↓ ↓H␈↓implementation␈α⊂will␈α⊂pose␈α⊂problems.␈α⊂ He␈α⊂will␈α⊂be␈α⊂right.␈α⊂ The␈α⊂internal␈α⊂form␈α⊂of␈α⊃ ␈↓↓<x y . . . z>␈↓ ␈α⊂is
␈↓ ↓H␈↓ ␈↓¬(LIST X Y . . . Z)␈↓.
␈↓ ↓H␈↓␈↓ εβChapter I␈↓ 99
␈↓ ↓H␈↓ Compositions␈αof␈α␈↓αa␈↓␈αand␈α␈↓αd␈↓␈αare␈αwritten␈αby␈αconcatenating␈αthe␈αletters␈α␈↓αa␈↓␈αand␈α␈↓αd␈↓.␈α Thus,␈αit␈αis␈αeasily
␈↓ ↓H␈↓seen␈α
that␈α
␈↓αad|␈↓␈↓↓x␈↓␈α
denotes␈αthe␈α
second␈α
element␈α
of␈αthe␈α
list␈α
␈↓↓x,␈↓␈α
and␈α
␈↓αadd|␈↓␈↓↓x␈↓␈αdenotes␈α
the␈α
third␈α
element␈αof␈α
that
␈↓ ↓H␈↓list. The internal names of these functions are ␈↓¬CADR ␈↓ and ␈↓¬CADDR ␈↓respectively.
␈↓ ↓H␈↓ Besides␈α
the␈α
basic␈α
functions␈αof␈α
LISP,␈α
there␈α
will␈αbe␈α
user-defined␈α
functions.␈α
We␈αhaven't␈α
given
␈↓ ↓H␈↓the␈α∞mechanism␈α
for␈α∞function␈α
definition␈α∞yet,␈α
but␈α∞suppose␈α
a␈α∞function␈α
␈↓↓subst␈↓␈α∞taking␈α∞three␈α
arguments
␈↓ ↓H␈↓has␈α→been␈α~defined.␈α→ It␈α→may␈α~be␈α→used␈α~in␈α→terms␈α→like␈α~ ␈↓↓subst[x,y,z]␈↓ ␈α→having␈α~internal␈α→form
␈↓ ↓H␈↓ ␈↓¬(SUBST X Y Z)␈↓.
␈↓ ↓H␈↓ As␈α∪in␈α∩other␈α∪programming␈α∩languages␈α∪and␈α∩in␈α∪mathematics␈α∩generally,␈α∪new␈α∩terms␈α∪can␈α∩be
␈↓ ↓H␈↓constructed␈α∂by␈α∂applying␈α∂functions␈α∂and␈α∂predicates␈α∂to␈α∂other␈α∂terms␈α∂and␈α∂not␈α∂just␈α∂to␈α∂variables␈α∂and
␈↓ ↓H␈↓constants.␈α⊂ Thus␈α⊂we␈α⊂have␈α⊂terms␈α⊂like␈α∂ ␈↓↓subst[x,y,␈↓αa|␈↓↓z]␈α⊂.␈α⊂subst[x,y,␈↓αd|␈↓↓z]␈↓ ␈α⊂involving␈α⊂a␈α⊂user␈α∂defined
␈↓ ↓H␈↓function ␈↓↓subst.␈↓ Its internal form is ␈↓¬(CONS (SUBST X Y (CAR Z)) (SUBST X Y (CDR Z)))␈↓.
␈↓ ↓H␈↓ In␈α
external␈α
language,␈α∞we␈α
often␈α
omit␈α∞brackets␈α
for␈α
functions␈α∞of␈α
one␈α
argument.␈α∞ Thus␈α
␈↓↓alt x␈↓
␈↓ ↓H␈↓stands␈α∞for␈α∞ ␈↓↓alt[x]␈↓, ␈α∞and␈α∞ ␈↓↓alt alt x␈↓ ␈α∞stands␈α∞for␈α∞ ␈↓↓alt[alt[x]]␈↓ .␈α∞ This␈α∞convention␈α∞was␈α∞also␈α∞used␈α∞in␈α∞the
␈↓ ↓H␈↓descriptions of ␈↓αa␈↓, ␈↓αd␈↓, ␈↓αat␈↓, and ␈↓αn␈↓.
␈↓ ↓H␈↓6. ␈↓αConditional expressions.␈↓
␈↓ ↓H␈↓ When␈α∩the␈α∩value␈α⊃of␈α∩a␈α∩function␈α∩depends␈α⊃on␈α∩whether␈α∩a␈α∩certain␈α⊃predicate␈α∩is␈α∩true␈α∩of␈α⊃the
␈↓ ↓H␈↓arguments, conditional terms are used to describe the function. The conditional term
␈↓ ↓H␈↓6.1)␈↓ ¬/␈↓↓␈↓αif␈↓↓ p ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b ␈↓
␈↓ ↓H␈↓is␈αevaluated␈αas␈αfollows:␈αfirst␈α ␈↓↓p␈↓␈α is␈αevaluated␈αand␈αdetermined␈αto␈αbe␈αtrue␈αor␈αfalse.␈α If␈α␈↓↓p␈↓␈αis␈αtrue,␈α
then
␈↓ ↓H␈↓␈↓↓a␈↓␈α
is␈α
evaluated␈α
and␈α
its␈α
value␈α
is␈α
the␈α
value␈α
of␈α
the␈α
expression.␈α
If␈α
␈↓↓p␈↓␈α
is␈α
false,␈α
then␈α
␈↓↓b␈↓␈α
is␈α
evaluated␈αand␈α
its
␈↓ ↓H␈↓value␈αis␈αthe␈αvalue␈αof␈αthe␈αexpression.␈α Note␈αthat␈αif␈α␈↓↓p␈↓␈αis␈αtrue,␈α␈↓↓b␈↓␈αis␈αnever␈αevaluated,␈αand␈αif␈α␈↓↓p␈↓␈αis␈αfalse,
␈↓ ↓H␈↓then␈α␈↓↓a␈↓␈α
is␈αnever␈α
evaluated.␈α The␈αimportance␈α
of␈αthis␈α
will␈αbe␈αexplained␈α
later.␈α Notice␈α
also␈αthat␈α
␈↓↓p␈↓␈αis
␈↓ ↓H␈↓expected␈α
to␈α
have␈α
the␈αvalue␈α
␈↓αtrue␈↓␈α
or␈α
␈↓αfalse␈↓,␈αin␈α
fact␈α
␈↓↓p␈↓␈α
should␈α
be␈αa␈α
propositional␈α
(or␈α
Boolean)␈αterm.␈α
A
␈↓ ↓H␈↓predicate␈α
applied␈α
to␈α
a␈α
list␈α
of␈α
arguments␈α
is␈α
a␈α
propositional␈α
term.␈α
We␈α
will␈α
explain␈α
how␈α
to␈α
form␈α
such
␈↓ ↓H␈↓terms␈α
in␈αgeneral␈α
in␈α
the␈αnext␈α
section.␈α A␈α
familiar␈α
function␈αthat␈α
can␈αbe␈α
written␈α
conveniently␈αusing
␈↓ ↓H␈↓conditional expressions is the absolute value of a real number. We have
␈↓ ↓H␈↓6.2)␈↓ ¬≤␈↓↓|x| = ␈↓αif␈↓↓ x<0 ␈↓αthen␈↓↓ -x ␈↓αelse␈↓↓ x␈↓.
␈↓ ↓H␈↓More generally a conditional term has the form
␈↓ ↓H␈↓6.3)␈↓ β↑␈↓↓␈↓αif␈↓↓ p ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ ␈↓αif␈↓↓ q ␈↓αthen␈↓↓ b . . . ␈↓αelse␈↓↓ ␈↓αif␈↓↓ s ␈↓αthen␈↓↓ d ␈↓αelse␈↓↓ e␈↓.
␈↓ ↓H␈↓There␈α∞can␈α∞be␈α∞any␈α∂number␈α∞of␈α∞clauses.␈α∞The␈α∞value␈α∂is␈α∞determined␈α∞by␈α∞evaluating␈α∂the␈α∞propositional
␈↓ ↓H␈↓terms␈α␈↓↓p,␈↓␈α␈↓↓q,␈↓␈αetc.␈αuntil␈αa␈αtrue␈αterm␈αis␈α
found,␈αthe␈αvalue␈αis␈αthen␈αthat␈αof␈αthe␈αterm␈αfollowing␈α
the␈αnext
␈↓ ↓H␈↓␈↓αthen␈↓.␈α If␈αnone␈αof␈αthe␈α
propositional␈αterms␈αis␈αtrue,␈αthen␈αthe␈α
value␈αis␈αthat␈αof␈αthe␈αterm␈α
following␈αthe
␈↓ ↓H␈↓last ␈↓αelse␈↓.
␈↓ ↓H␈↓ Figure␈α_6␈α_shows␈α→an␈α_example␈α_of␈α_a␈α→(numeric)␈α_function␈α_defined␈α_using␈α→a␈α_conditional
␈↓ ↓H␈↓expression.
␈↓ ↓H␈↓10␈↓ εβChapter I␈↓ H
␈↓"␈↓ ↓H␈↓ (0,1)
␈↓"␈↓ ↓H␈↓ ≤'`≥
␈↓"␈↓ ↓H␈↓ ≤' `≥
␈↓"␈↓ ↓H␈↓ ↑ ≤' `≥
␈↓"␈↓ ↓H␈↓ ~ ≤' `≥
␈↓"␈↓ ↓H␈↓ tri[x] ~ αααααααααααααααα' `αααααααααααααααα
␈↓"␈↓ ↓H␈↓ ~ (-1,0) (1,0)
␈↓"␈↓ ↓H␈↓ ααα→
␈↓"␈↓ ↓H␈↓ x
␈↓"␈↓ ↓H␈↓␈↓ αr␈↓↓tri[x] = ␈↓αif␈↓↓ x<-1 ␈↓αthen␈↓↓ 0 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ x<0 ␈↓αthen␈↓↓ 1+x ␈↓αelse␈↓↓ ␈↓αif␈↓↓ x<1 ␈↓αthen␈↓↓ 1-x ␈↓αelse␈↓↓ 0␈↓.
␈↓"␈↓ ↓H␈↓␈↓ ε␈↓Figure 6.␈↓
␈↓ ↓H␈↓ The conditional term in internal language has the form
␈↓ ↓H␈↓6.4)␈↓ ∧o␈↓↓␈↓¬(COND ␈↓↓(p1 e1) (p2 e2) ... (pn en)␈↓¬)␈↓↓␈↓
␈↓ ↓H␈↓with␈α
any␈α
number␈αof␈α
␈↓↓p-e␈↓␈α
pairs.␈α
Its␈αvalue␈α
is␈α
determined␈α
by␈αevaluating␈α
the␈α
␈↓↓p␈↓'s␈α
successively␈αuntil␈α
one
␈↓ ↓H␈↓is␈α∃found␈α∃with␈α∃a␈α∃value␈α∃representing␈α∃␈↓αtrue␈↓␈α⊗(␈↓¬T␈↓␈α∃by␈α∃our␈α∃convention).␈α∃ Then␈α∃the␈α∃value␈α⊗of␈α∃the
␈↓ ↓H␈↓corresponding␈α␈↓↓e␈↓␈αis␈αtaken␈αas␈αthe␈αvalue␈αof␈αthe␈αconditional␈αterm.␈α If␈αnone␈αof␈αthe␈α␈↓↓p␈↓'s␈αis␈αtrue,␈αthen␈αthe
␈↓ ↓H␈↓value␈α⊂of␈α⊂the␈α⊂conditional␈α∂term␈α⊂is␈α⊂undefined.␈α⊂ (In␈α∂some␈α⊂implementations␈α⊂the␈α⊂conditional␈α⊂term␈α∂is
␈↓ ↓H␈↓given␈α
a␈α
default␈α
value␈α
such␈α
as␈α
␈↓¬NIL␈↓␈α
if␈α
none␈α
of␈α
the␈α
␈↓↓p␈↓'s␈α
are␈α
true.) ␈α
Here␈α
all␈α
the␈α
␈↓↓e␈↓'s␈α
are␈α
treated␈α
the␈α
same
␈↓ ↓H␈↓which␈α
makes␈αprograms␈α
for␈α
interpreting␈αor␈α
compiling␈α
conditional␈αexpressions␈α
easier␈α
to␈αwrite.␈α
Thus
␈↓ ↓H␈↓in␈α∂internal␈α∞language␈α∂conditional␈α∂expressions␈α∞are␈α∂made␈α∂in␈α∞a␈α∂more␈α∂regular␈α∞way␈α∂than␈α∂in␈α∞external
␈↓ ↓H␈↓language.␈α∂The␈α∂external␈α⊂form␈α∂was␈α∂altered␈α∂to␈α⊂conform␈α∂to␈α∂ALGOL.␈α∂ Putting␈α⊂parentheses␈α∂around
␈↓ ↓H␈↓each␈α
␈↓↓p-e␈↓␈α
pair␈α
was␈α
probably␈α
a␈α
mistake␈α
in␈α
the␈α
design␈α
of␈α
LISP,␈α
because␈α
it␈α
unnecessarily␈αincreases␈α
the
␈↓ ↓H␈↓number␈α∞of␈α
parentheses.␈α∞ It␈α
should␈α∞have␈α
been␈α∞ ␈↓¬(COND ␈↓↓p1 e1 p2 e2 ... pn en␈↓), ␈α
but␈α∞such␈α∞a␈α
change
␈↓ ↓H␈↓should be made now only as part of a massive improvement.
␈↓ ↓H␈↓ Conditional expressions may be compounded with functions to get terms like
␈↓ ↓H␈↓6.5)␈↓ ∧2␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . append[␈↓αd|␈↓↓u, v] ␈↓
␈↓ ↓H␈↓involving a yet to be defined function ␈↓↓append.␈↓ The internal form of this is
␈↓ ↓H␈↓6.6)␈↓ βI␈↓↓␈↓¬(COND ((NULL U) V) (T (CONS (CAR U) (APPEND (CDR U) V))))␈↓↓.␈↓
␈↓ ↓H␈↓One␈α
would␈α
normally␈α∞have␈α
expected␈α
to␈α∞write␈α
␈↓¬(QUOTE␈α
T)␈↓,␈α∞but␈α
there␈α
is␈α∞a␈α
special␈α
convention␈α∞that␈α
␈↓¬T␈↓
␈↓ ↓H␈↓may␈α⊂be␈α⊃written␈α⊂without␈α⊃␈↓¬QUOTE.␈α⊂␈↓␈α⊃This␈α⊂convention␈α⊃applies␈α⊂to␈α⊃␈↓¬NIL␈↓␈α⊂also.␈α⊃ This␈α⊂means␈α⊃that␈α⊂these
␈↓ ↓H␈↓symbols␈α∞cannot␈α∞be␈α∞used␈α∂as␈α∞variables.␈α∞ As␈α∞mentioned␈α∞in␈α∂the␈α∞preceeding␈α∞section,␈α∞␈↓¬T␈↓␈α∂represents␈α∞the
␈↓ ↓H␈↓propositional␈α
constant␈α␈↓αtrue␈↓,␈α
i.e.␈αit␈α
is␈αalways␈α
true␈αso␈α
that␈αit␈α
is␈αimpossible␈α
to␈α"fall␈α
off␈αthe␈α
end"␈α
of␈αa
␈↓ ↓H␈↓␈↓ εβChapter I␈↓ *11
␈↓ ↓H␈↓conditional␈α
expression␈αwith␈α
␈↓¬T␈↓␈αas␈α
its␈αlast␈α
␈↓↓p.␈↓␈α It␈α
is␈α
the␈αtranslation␈α
into␈αinternal␈α
form␈αof␈α
the␈αfinal␈α
␈↓αelse␈↓
␈↓ ↓H␈↓of a conditional expression in external form.
␈↓ ↓H␈↓7. ␈↓αPropositional expressions ␈↓
␈↓ ↓H␈↓ Propositional␈α
terms␈α
are␈α
a␈α
subset␈α
of␈α
the␈α
class␈α
of␈α
expressions␈α
evaluating␈α
to␈α
␈↓αtrue␈↓␈α
or␈α
␈↓αfalse␈↓␈α
rather
␈↓ ↓H␈↓than␈α∞having␈α
S-expressions␈α∞as␈α
values.␈α∞ As␈α∞mentioned␈α
earlier,␈α∞if␈α
␈↓πf␈↓␈α∞is␈α
a␈α∞predicate␈α∞of␈α
n-arguments
␈↓ ↓H␈↓then␈α
␈↓↓␈↓πf␈↓↓[x1, . . . ,xn]␈↓␈α
is␈α
a␈α
propositional␈α
term.␈α Additional␈α
propositional␈α
terms␈α
can␈α
be␈α
formed␈αby
␈↓ ↓H␈↓combining␈α∞already␈α∞formed␈α∞ones␈α∞using␈α∞the␈α∞logical␈α∞operations␈α∞of␈α∞conjunction(and),␈α∞disjunction(or)
␈↓ ↓H␈↓and␈α∂negation(not).␈α∂ We␈α∞will␈α∂use␈α∂the␈α∂symbols␈α∞∧,␈α∂∨,␈α∂and␈α∂¬␈α∞respectively␈α∂for␈α∂these␈α∂operators.␈α∞ The
␈↓ ↓H␈↓value␈αof␈αa␈αpropositional␈αterm␈αcan␈αbe␈αdetermined␈αusing␈αthe␈αtruth␈αtable␈αgiven␈αin␈αfigure␈α7.␈α Both␈α∧
␈↓ ↓H␈↓and␈α∨␈αare␈αassociative,␈αso␈αwe␈αcan␈αwrite␈αexpressions␈αlike␈α ␈↓↓p1 ∧ p2 ∧ p3␈↓ ␈αwithout␈αworrying␈αabout␈α
the
␈↓ ↓H␈↓grouping.
␈↓ ↓H␈↓␈↓ α8␈↓αtrue␈↓ ∧ ␈↓αtrue␈↓ = ␈↓αtrue␈↓␈↓ ¬X␈↓αtrue␈↓ ∨ ␈↓αtrue␈↓ = ␈↓αtrue␈↓
␈↓ ↓H␈↓␈↓ α8␈↓αtrue␈↓ ∧ ␈↓αfalse␈↓ = ␈↓αfalse␈↓␈↓ ¬X␈↓αtrue␈↓ ∨ ␈↓αfalse␈↓ = ␈↓αtrue␈↓␈↓ λx¬␈↓αtrue␈↓ = ␈↓αfalse␈↓
␈↓ ↓H␈↓␈↓ α8␈↓αfalse␈↓ ∧ ␈↓αtrue␈↓ = ␈↓αfalse␈↓␈↓ ¬X␈↓αfalse␈↓ ∨ ␈↓αtrue␈↓ = ␈↓αtrue␈↓␈↓ λx¬␈↓αfalse␈↓ = ␈↓αtrue␈↓
␈↓ ↓H␈↓␈↓ α8␈↓αfalse␈↓ ∧ ␈↓αfalse␈↓ = ␈↓αfalse␈↓␈↓ ¬X␈↓αfalse␈↓ ∨ ␈↓αfalse␈↓ = ␈↓αfalse␈↓
␈↓ ↓H␈↓␈↓ ε␈↓Figure 7.␈↓
␈↓ ↓H␈↓ In␈α∞order␈α∞to␈α∞compute␈α∞the␈α∞value␈α∞of␈α∞propositional␈α∞terms␈α∞we␈α∞introduce␈α∞the␈α∞operators␈α∞␈↓αand␈↓,␈α∞␈↓αor␈↓,
␈↓ ↓H␈↓and ␈↓αnot␈↓. These operators are defined using conditional terms as follows:
␈↓ ↓H␈↓7.1)␈↓ ¬λ␈↓↓p ␈↓αand␈↓↓ q = ␈↓αif␈↓↓ p ␈↓αthen␈↓↓ q ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓7.2)␈↓ ¬≠␈↓↓ p ␈↓αor␈↓↓ q = ␈↓αif␈↓↓ q ␈↓αthen␈↓↓ ␈↓¬T␈↓↓ ␈↓αelse␈↓↓ q␈↓
␈↓ ↓H␈↓7.3)␈↓ ¬
␈↓↓ ␈↓αnot␈↓↓ p = ␈↓αif␈↓↓ p ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓¬T␈↓↓␈↓
␈↓ ↓H␈↓The internal form of these definitions is
␈↓ ↓H␈↓␈↓ ∧|␈↓↓␈↓¬(AND P Q) = (COND (P Q) (T NIL))␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧h␈↓↓␈↓¬(OR P Q) = (COND (P T) (T Q)) ␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬↓␈↓↓␈↓¬ (NOT P) = (COND (P NIL) (T T))␈↓↓␈↓
␈↓ ↓H␈↓Note␈α∞that␈α
if␈α∞␈↓↓p␈↓␈α
is␈α∞false␈α
then␈α∞ ␈↓↓p ␈↓αand␈↓↓ q = ␈↓¬NIL␈↓↓␈↓ ␈α∞independent␈α
of␈α∞the␈α
value␈α∞of␈α
␈↓↓q,␈↓␈α∞and␈α
likewise␈α∞if␈α∞␈↓↓p␈↓␈α
is
␈↓ ↓H␈↓true,␈α
then␈α ␈↓↓p ␈↓αor␈↓↓ q = ␈↓¬T␈↓↓␈↓ ␈α
independent␈αof␈α
␈↓↓q.␈↓␈α If␈α
␈↓↓p␈↓␈αhas␈α
been␈αevaluated␈α
and␈αfound␈α
to␈αbe␈α
false,␈α
then␈α␈↓↓q␈↓
␈↓ ↓H␈↓does␈α∞not␈α∞have␈α∞to␈α∞be␈α∞evaluated␈α∞at␈α∞all␈α∞to␈α∂find␈α∞the␈α∞value␈α∞of␈α∞␈↓↓p ␈↓αand␈↓↓ q␈↓,␈α∞and,␈α∞in␈α∞fact,␈α∞LISP␈α∂does␈α∞not
␈↓ ↓H␈↓evaluate␈α⊃␈↓↓q␈↓␈α⊃in␈α⊃this␈α⊂case.␈α⊃ Similarly,␈α⊃␈↓↓q␈↓␈α⊃is␈α⊂not␈α⊃evaluated␈α⊃in␈α⊃evaluating␈α⊂␈↓↓p ␈↓αor␈↓↓ q␈↓␈α⊃if␈α⊃␈↓↓p␈↓␈α⊃is␈α⊃true.␈α⊂This
␈↓ ↓H␈↓procedure␈α∪is␈α∀in␈α∪accordance␈α∪with␈α∀the␈α∪above␈α∪conditional␈α∀expression␈α∪descriptions␈α∪of␈α∀the␈α∪these
␈↓ ↓H␈↓12␈↓ εβChapter I␈↓ H
␈↓ ↓H␈↓operators.␈α
The␈α
importance␈α
of␈αthis␈α
convention,␈α
which␈α
contrasts␈α
with␈αthat␈α
of␈α
ALGOL␈α
60,␈α
will␈αbe
␈↓ ↓H␈↓apparent␈αlater␈αwhen␈αwe␈αdiscuss␈αrecursive␈αdefinitions␈αof␈αfunctions␈αand␈αpredicates.␈α Thus,␈αalthough
␈↓ ↓H␈↓we␈αwill␈αcontinue␈α
to␈αuse␈αthe␈α
symbols␈α∧,␈α∨,␈α
and␈α¬␈αin␈α
expressing␈αpropostional␈αterms,␈αwhen␈α
evaluating
␈↓ ↓H␈↓LISP␈αterms␈αwe␈αwill␈αuse␈αthe␈αrules␈αfor␈α␈↓αand␈↓,␈α␈↓αor␈↓,␈αand␈α␈↓αnot␈↓␈αgiven␈αabove.␈α Note␈αthat␈αwhen␈α␈↓↓p␈↓␈αand␈α␈↓↓q␈↓␈αcan
␈↓ ↓H␈↓both␈α∩be␈α∪evaluated,␈α∩the␈α∩operators␈α∪␈↓αand␈↓␈α∩␈↓αor␈↓␈α∩and␈α∪␈↓αnot␈↓␈α∩are␈α∩representing␈α∪functions␈α∩for␈α∪the␈α∩logical
␈↓ ↓H␈↓operators ∧, ∨, and ¬ (eg. ␈↓↓p ␈↓αand␈↓↓ q = ␈↓¬T␈↓↓ [␈↓¬NIL␈↓↓]␈↓ precisely when ␈↓↓p ∧ q ≡ ␈↓αtrue␈↓↓ [␈↓αfalse␈↓↓]␈↓).
␈↓ ↓H␈↓ Propositional␈α
expressions␈α
can␈α
be␈α
combined␈α
with␈α
functions␈α
and␈α
conditional␈α
expressions␈α
to␈α
get
␈↓ ↓H␈↓terms like
␈↓ ↓H␈↓7.4)␈↓ ∧E␈↓↓␈↓αif␈↓↓ [␈↓αn|␈↓↓u ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓u] ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . alt ␈↓αdd|␈↓↓u␈↓
␈↓ ↓H␈↓for which the translation into internal form is
␈↓ ↓H␈↓7.5)␈↓ αk␈↓↓␈↓¬(COND ((OR (NULL U) (NULL (CDR U))) U) (T (CONS (CAR U) (ALT (CDDR U)))))␈↓↓␈↓.
␈↓ ↓H␈↓8. ␈↓αRecursive function definitions.␈↓
␈↓ ↓H␈↓ In␈α∞such␈α∞languages␈α
as␈α∞FORTRAN␈α∞and␈α
ALGOL,␈α∞computer␈α∞programs␈α
are␈α∞expressed␈α∞in␈α
the
␈↓ ↓H␈↓main␈α⊃as␈α⊃sequences␈α⊃of␈α⊃assignment␈α⊃statements␈α⊃and␈α⊃conditional␈α⊃␈↓αgo␈α⊃to␈↓'s.␈α⊃ In␈α⊃LISP,␈α⊃programs␈α⊃are
␈↓ ↓H␈↓mainly expressed in the form of recursively defined functions. We begin with an example.
␈↓ ↓H␈↓ We␈αwant␈α
a␈αfunction␈α␈↓↓alt␈↓␈α
such␈αthat␈α␈↓↓alt u␈↓␈α
gives␈αa␈α
list␈αwhose␈αelements␈α
are␈αalternate␈αelements␈α
of
␈↓ ↓H␈↓the list ␈↓↓u␈↓ beginning with the first. For example we want
␈↓ ↓H␈↓␈↓ ¬≠␈↓↓ alt ␈↓¬(A B C D E)␈↓↓ = ␈↓¬(A C E)␈↓↓␈↓,
␈↓ ↓H␈↓␈↓ ¬#␈↓↓ alt ␈↓¬((A B) (C D))␈↓↓ = ␈↓¬((A B))␈↓↓␈↓,
␈↓ ↓H␈↓␈↓ ¬-␈↓↓ alt ␈↓¬(A)␈↓↓ = ␈↓¬(A)␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ ¬~␈↓↓ alt ␈↓¬NIL␈↓↓ = ␈↓¬NIL␈↓↓. ␈↓
␈↓ ↓H␈↓and in LISP we can define ␈↓↓alt␈↓ by the recursion equation
␈↓ ↓H␈↓8.1) ␈↓ ∧~␈↓↓alt u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . alt ␈↓αdd|␈↓↓u␈↓.
␈↓ ↓H␈↓In case you need a review of our precedence conventions, fully bracketed it looks like
␈↓ ↓H␈↓␈↓ βJ␈↓↓alt[u] ← ␈↓αif␈↓↓ [␈↓αn|␈↓↓[u] ∨ ␈↓αn|␈↓↓[␈↓αd|␈↓↓[u]]] ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ [␈↓αa|␈↓↓[u] . alt[␈↓αdd|␈↓↓[u]]]␈↓.
␈↓ ↓H␈↓ The␈α⊃terms␈α∩appearing␈α⊃in␈α⊃the␈α∩recursion␈α⊃equation␈α⊃are␈α∩formed␈α⊃by␈α⊃the␈α∩methods␈α⊃previously
␈↓ ↓H␈↓discussed.␈α Notice␈αthat␈αthe␈αfunction␈α␈↓↓alt␈↓␈αoccurs␈αin␈αthe␈αright␈αhand␈αside␈αof␈αits␈αown␈αdefining␈αequation
␈↓ ↓H␈↓and␈αthat␈αwe␈αuse␈α
"←"␈αinstead␈αof␈α"="␈α.␈α
The␈αrecursion␈αequation␈αmeans␈αthat␈α
␈↓↓alt␈↓␈αis␈αa␈αfunction␈α
of␈αone
␈↓ ↓H␈↓variable␈α∞␈↓↓u,␈↓␈α∞and␈α∞that␈α∞the␈α∞value␈α∞of␈α∞␈↓↓alt␈α∞u␈↓␈α∞for␈α∞some␈α∞particular␈α∞list␈α∞is␈α∞computed␈α∞by␈α∂evaluating␈α∞the
␈↓ ↓H␈↓right␈α
hand␈α
side␈α
of␈α
the␈αdefinition,␈α
substituting␈α
that␈α
list␈α
for␈α␈↓↓u␈↓␈α
whenever␈α
␈↓↓u␈↓␈α
is␈α
encountered␈α
and␈αre-
␈↓ ↓H␈↓evaluating␈αthe␈αright␈αhand␈αside␈αwith␈αa␈αnew␈α␈↓↓u␈↓␈αwhenever␈αa␈αterm␈α␈↓↓alt␈αe␈↓␈αis␈αencountered.␈α This␈αprocess
␈↓ ↓H␈↓is best illsutrated by evaluating some expressions.
␈↓ ↓H␈↓␈↓ εβChapter I␈↓ *13
␈↓ ↓H␈↓ Consider␈αevaluating␈α␈↓↓alt ␈↓¬NIL␈↓↓␈↓.␈α We␈αdo␈αit␈αby␈αevaluating␈αthe␈αexpression␈αto␈αthe␈αright␈αof␈αthe␈α
"←"
␈↓ ↓H␈↓remembering␈αthat␈αthe␈α
variable␈α␈↓↓u␈↓␈αhas␈αthe␈α
value␈α␈↓¬NIL␈↓.␈α A␈αconditional␈α
expression␈αis␈αevaluated␈αby␈α
first
␈↓ ↓H␈↓evaluating␈αthe␈αfirst␈αpropositional␈αterm␈α-␈αin␈αthis␈αcase␈α␈↓↓␈↓αn|␈↓↓u␈α∨␈α␈↓αn|␈↓↓␈↓αd|␈↓↓u␈↓.␈αThis␈αexpression␈αis␈αa␈αdisjunction
␈↓ ↓H␈↓so␈α
we␈α∞first␈α
evaluate␈α∞the␈α
first␈α
disjunct,␈α∞namely␈α
␈↓αn|␈↓␈↓↓u.␈↓␈α∞(Recall␈α
the␈α
convention␈α∞given␈α
in␈α∞the␈α
previous
␈↓ ↓H␈↓section).␈α∂ Since␈α∂ ␈↓↓u␈α∞=␈α∂␈↓¬NIL␈↓↓␈↓,␈α∂ ␈↓↓␈↓αn|␈↓↓u␈↓ ␈α∞is␈α∂true,␈α∂the␈α∂disjunction␈α∞is␈α∂true,␈α∂and␈α∞the␈α∂value␈α∂of␈α∂the␈α∞conditional
␈↓ ↓H␈↓expression␈αis␈αthe␈αvalue␈αof␈αthe␈αterm␈αafter␈αthe␈αfirst␈αtrue␈αpropositional␈αterm.␈αThe␈αterm␈αis␈α␈↓↓u,␈↓␈αand␈αits
␈↓ ↓H␈↓value␈αis␈α␈↓¬NIL␈↓,␈α
so␈αthe␈αvalue␈αof␈α
␈↓↓alt[␈↓¬NIL␈↓↓]␈↓ ␈αis␈α␈↓¬NIL␈↓␈αas␈α
stated␈αabove.␈α Obeying␈αthe␈α
rules␈αabout␈αthe␈αorder␈α
of
␈↓ ↓H␈↓evaluation␈α
of␈α
terms␈α
in␈α
conditional␈α
and␈α
Boolean␈α
expressions␈α
is␈α
important␈α
in␈α
this␈α
case␈α
since␈α
if␈αwe
␈↓ ↓H␈↓didn't␈αobey␈αthese␈α
rules,␈αwe␈αmight␈α
find␈αourselves␈αtrying␈α
to␈αevaluate␈α ␈↓↓␈↓αn|␈↓↓␈↓αd|␈↓↓u␈↓ ␈α
or␈α ␈↓↓alt[␈↓αdd|␈↓↓u]␈↓,␈αand␈αsince␈α
␈↓↓u␈↓
␈↓ ↓H␈↓is␈α␈↓¬NIL␈↓,␈αneither␈αof␈α
these␈αhas␈αa␈αvalue.␈α An␈α
attempt␈αto␈αevaluate␈αthem␈αin␈α
LISP␈αwould␈αgive␈αrise␈α
to␈αan
␈↓ ↓H␈↓error return.
␈↓ ↓H␈↓ As␈αa␈αsecond␈α
example,␈αconsider␈α ␈↓↓alt ␈↓¬(A B)␈↓↓␈↓.␈α
Since␈αneither␈α␈↓αn|␈↓␈↓↓u␈↓␈α
nor␈α␈↓αn|␈↓␈↓αd|␈↓␈↓↓u␈↓␈αis␈α
true␈αin␈αthis␈αcase,␈α
the
␈↓ ↓H␈↓disjunction␈α ␈↓↓␈↓αn|␈↓↓u␈α
∨␈α␈↓αn|␈↓↓␈↓αd|␈↓↓u␈↓ ␈α
is␈αfalse␈α
and␈αthe␈α
value␈αof␈α
the␈αexpression␈α
is␈αthe␈α
value␈αof␈α ␈↓↓␈↓αa|␈↓↓u . alt ␈↓αdd|␈↓↓u␈↓.␈α
␈↓αa|␈↓␈↓↓u␈↓
␈↓ ↓H␈↓has␈α∞the␈α∂value␈α∞␈↓¬A,␈α∞␈↓and␈α∂␈↓αdd|␈↓␈↓↓u␈↓␈α∞has␈α∞the␈α∂value␈α∞␈↓¬NIL␈↓,␈α∞so␈α∂we␈α∞must␈α∞now␈α∂evaluate␈α∞␈↓↓alt ␈↓¬NIL␈↓↓␈↓,␈α∞and␈α∂we␈α∞already
␈↓ ↓H␈↓know that this is ␈↓¬NIL␈↓. Therefore, the value of ␈↓↓alt ␈↓¬(A B)␈↓↓␈↓ is that of ␈↓¬A . NIL␈↓ which is ␈↓¬(A)␈↓.
␈↓ ↓H␈↓ We can describe this evaluation as a sequence of equations as follows
␈↓ ↓H␈↓␈↓ β8␈↓↓alt ␈↓¬(A B)␈↓↓␈↓␈↓ ∧8␈↓↓= ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓¬(A B)␈↓↓ ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬(A B)␈↓↓ ␈↓αthen␈↓↓ ␈↓¬(A B)␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬(A B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓= ␈↓αif␈↓↓ ␈↓¬NIL␈↓↓ ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬(A B)␈↓↓ ␈↓αthen␈↓↓ ␈↓¬(A B)␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬(A B)␈↓↓] ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓= ␈↓αif␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αthen␈↓↓ ␈↓¬(A B)␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬(A B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓= ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬(A B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓= ␈↓¬A ␈↓↓. alt[␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓= ␈↓¬A ␈↓↓. [␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓¬NIL␈↓↓ ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬NIL␈↓↓ ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬NIL␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬NIL␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓= ␈↓¬A ␈↓↓. [␈↓αif␈↓↓ ␈↓¬T␈↓↓ ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬NIL␈↓↓ ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬NIL␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬NIL␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓= ␈↓¬A ␈↓↓. [␈↓αif␈↓↓ ␈↓¬T␈↓↓ ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬NIL␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬NIL␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓= ␈↓¬A ␈↓↓. [␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓= ␈↓¬(A)␈↓↓␈↓.
␈↓ ↓H␈↓ This␈αis␈α
still␈αvery␈α
long-winded,␈αand␈αnow␈α
that␈αthe␈α
reader␈αunderstands␈α
the␈αorder␈αof␈α
evaluation
␈↓ ↓H␈↓of conditional and Boolean expressions, we can proceed more briefly to evaluate
␈↓ ↓H␈↓␈↓ β8␈↓↓alt[␈↓¬(A B C D E)␈↓↓]␈↓␈↓ ¬_␈↓↓= ␈↓¬A ␈↓↓. alt[␈↓¬(C D E)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬A ␈↓↓. [␈↓¬C ␈↓↓. alt[␈↓¬(E)␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬A ␈↓↓. [␈↓¬(C E)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬(A C E)␈↓↓␈↓.
␈↓ ↓H␈↓ Here␈αare␈αthree␈αmore␈αexamples␈αof␈αrecursive␈αfunctions␈αand␈αtheir␈αapplication.␈α We␈αdefine␈α␈↓↓last␈↓
␈↓ ↓H␈↓by
␈↓ ↓H␈↓8.2) ␈↓ ∧U␈↓↓last u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ ␈↓αa|␈↓↓u ␈↓αelse␈↓↓ last ␈↓αd|␈↓↓u␈↓,
␈↓ ↓H␈↓and we compute
␈↓ ↓H␈↓14␈↓ εβChapter I␈↓ H
␈↓ ↓H␈↓␈↓ βx␈↓↓last ␈↓¬(A B C)␈↓↓␈↓␈↓ ¬_␈↓↓= ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬(A B C)␈↓↓ ␈↓αthen␈↓↓ ␈↓αa|␈↓↓␈↓¬(A B C)␈↓↓ ␈↓αelse␈↓↓ last ␈↓αd|␈↓↓␈↓¬(A B C)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= last ␈↓¬(B C)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= last ␈↓¬(C)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬C␈↓↓␈↓.
␈↓ ↓H␈↓Clearly␈α ␈↓↓last␈↓␈α computes␈αthe␈αlast␈αelement␈αof␈αa␈αlist.␈α ␈↓↓last ␈↓¬NIL␈↓↓␈↓␈αis␈αundefined␈αin␈αthe␈αLISP␈αlanguage;␈αthe
␈↓ ↓H␈↓result␈αof␈αtrying␈αto␈αcompute␈αit␈αmay␈αbe␈αan␈αerror␈αmessage␈αor␈αmay␈αbe␈αsome␈αrandom␈αresult␈αdepending
␈↓ ↓H␈↓on␈αthe␈αimplementation.␈α (A␈αheavy␈αduty␈αversion␈αof␈α␈↓↓last␈↓␈αwould␈αexplicitly␈αcall␈αa␈αfunction␈αcalled␈α␈↓↓error␈↓
␈↓ ↓H␈↓with a string expressing the complaint that the program had tried to compute ␈↓↓last␈↓ ␈↓¬NIL␈↓.)
␈↓ ↓H␈↓ The function ␈↓↓subst␈↓ is defined by
␈↓ ↓H␈↓8.3) ␈↓ α∂␈↓↓subst[x, y, z] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓z ␈↓αthen␈↓↓ [␈↓αif␈↓↓ z ␈↓αeq␈↓↓ y ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ z] ␈↓αelse␈↓↓ subst[x,y,␈↓αa|␈↓↓z] . subst[x,y,␈↓αd|␈↓↓z]␈↓.
␈↓ ↓H␈↓We have
␈↓ ↓H␈↓␈↓ αh␈↓↓subst[␈↓¬(A.B), X, ((X.A).X)␈↓↓] ␈↓␈↓ ¬_␈↓↓= subst[␈↓¬(A.B), X, (X.A)␈↓↓] . subst[␈↓¬(A.B), X, X␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= [subst[␈↓¬(A.B), X, X␈↓↓] . subst[␈↓¬(A.B), X, A␈↓↓]] . ␈↓¬(A.B)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= [[␈↓¬(A.B)␈↓↓].␈↓¬A␈↓↓].␈↓¬(A.B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬(((A.B).A).(A.B))␈↓↓␈↓.
␈↓ ↓H␈↓␈↓↓subst␈↓␈αcomputes␈αthe␈αresult␈αof␈αsubstituting␈αthe␈αS-expression␈α␈↓↓x␈↓␈αfor␈αthe␈αatom␈α␈↓↓y␈↓␈αin␈αthe␈αS-expression␈α␈↓↓z.␈↓
␈↓ ↓H␈↓This operation is important in all kinds of symbolic computation.
␈↓ ↓H␈↓ The␈α∩function␈α∩␈↓↓append[u,v]␈↓␈α∩which␈α∩gives␈α∩the␈α∪concatenation␈α∩of␈α∩the␈α∩lists␈α∩␈↓↓u␈↓␈α∩and␈α∩␈↓↓v␈↓␈α∪is␈α∩also
␈↓ ↓H␈↓important. It is also denoted by the infixed expression ␈↓↓u * v␈↓. For example we have
␈↓ ↓H␈↓␈↓ ∧p␈↓↓␈↓¬(A B C)␈↓↓ * ␈↓¬(D E F)␈↓↓ = ␈↓¬(A B C D E F)␈↓↓,␈↓
␈↓ ↓H␈↓␈↓ ∧x␈↓↓␈↓¬NIL␈↓↓ * ␈↓¬(A B)␈↓↓ =␈↓¬(A B)␈↓↓ = ␈↓¬(A B)␈↓↓ * ␈↓¬NIL␈↓↓,␈↓
␈↓ ↓H␈↓and the formal definition
␈↓ ↓H␈↓8.4) ␈↓ ∧C␈↓↓u * v ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u] * v␈↓.
␈↓ ↓H␈↓The␈α∞␈↓↓append␈↓␈α∞function␈α
is␈α∞machine␈α∞coded␈α
in␈α∞most␈α∞Lisp␈α
systems␈α∞in␈α∞a␈α
way␈α∞that␈α∞allows␈α∞an␈α
arbitrary
␈↓ ↓H␈↓number of arguments, e.g. ␈↓¬(APPEND (QUOTE (A B)) (QUOTE (C D)) (QUOTE (E F))) is (A B C D E F)␈↓.
␈↓ ↓H␈↓ The␈α⊂Boolean␈α∂operations␈α⊂can␈α⊂also␈α∂be␈α⊂used␈α∂in␈α⊂making␈α⊂recursive␈α∂definitions␈α⊂since␈α⊂we␈α∂take
␈↓ ↓H␈↓advantage of the order of evaluation of constituents. Thus, we define a predicate ␈↓↓equal␈↓ by
␈↓ ↓H␈↓8.5) ␈↓ β↓␈↓↓equal[x,y] ← x ␈↓αeq␈↓↓ y ∨ [¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y ∧ equal[␈↓αa|␈↓↓x,␈↓αa|␈↓↓y] ∧ equal[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y]]␈↓.
␈↓ ↓H␈↓␈↓↓equal[x,y]␈↓␈αis␈αtrue␈αif␈αand␈αonly␈αif␈α␈↓↓x␈↓␈αand␈α␈↓↓y␈↓␈αare␈αthe␈αsame␈αS-expression,␈αand␈αthe␈αuse␈αof␈αthis␈αpredicate
␈↓ ↓H␈↓makes␈αup␈αfor␈αthe␈αfact␈αthat␈αthe␈αbasic␈αpredicate␈α␈↓αeq␈↓␈αis␈αguaranteed␈αto␈αtest␈αequality␈αonly␈αwhen␈αone␈αof
␈↓ ↓H␈↓the operands is known to be an atom. We shall also use the infixes ␈↓α=␈↓ and ␈↓α≠␈↓.
␈↓ ↓H␈↓ Membership of an S-expression ␈↓↓x␈↓ in a list ␈↓↓u␈↓ is tested by
␈↓ ↓H␈↓␈↓ εβChapter I␈↓ *15
␈↓ ↓H␈↓8.6) ␈↓ β{␈↓↓member[x, u] ← ¬␈↓αn|␈↓↓u ∧ [[x = ␈↓αa|␈↓↓u] ∨ member[x, ␈↓αd|␈↓↓u]]␈↓.
␈↓ ↓H␈↓This relation is also denoted by ␈↓↓x ε u␈↓. Here are some computations:
␈↓ ↓H␈↓␈↓ β8␈↓↓member[␈↓¬B, ␈↓↓␈↓¬(A B)␈↓↓]␈↓␈↓ ¬_␈↓↓= ¬␈↓αn|␈↓↓␈↓¬(A B)␈↓↓ ∧ [[␈↓¬B ␈↓↓= ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓] ∨ member[␈↓¬B, ␈↓↓␈↓αd|␈↓↓␈↓¬(A B)␈↓↓]]␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= member[␈↓¬B, ␈↓↓␈↓¬(B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬T␈↓↓␈↓.
␈↓ ↓H␈↓ Sometimes␈α
a␈α
function␈α
is␈α
defined␈α
with␈α
the␈α
help␈α
of␈α
auxiliary␈α
functions.␈α
Thus,␈α
the␈α
reverse␈α
of␈α
a
␈↓ ↓H␈↓list ␈↓↓u␈↓ , (e.g. ␈↓↓reverse[␈↓¬(A B C D)␈↓↓] = ␈↓¬(D C B A)␈↓↓␈↓), is given by the pair of equations
␈↓ ↓H␈↓␈↓ ¬2␈↓↓reverse[u] ← rev[u, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓8.7)
␈↓ ↓H␈↓␈↓ ∧≥␈↓↓rev[u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ rev[␈↓αd|␈↓↓u, [␈↓αa|␈↓↓u].v]␈↓.
␈↓ ↓H␈↓A computation is:
␈↓ ↓H␈↓␈↓ β8␈↓↓reverse[␈↓¬(A B C)␈↓↓]␈↓␈↓ ¬_␈↓↓= rev[␈↓¬(A B C)␈↓↓, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= rev[␈↓¬(B C), (A)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= rev[␈↓¬(C), (B A)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= rev[␈↓¬NIL␈↓↓, ␈↓¬(C B A)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬(C B A)␈↓↓␈↓.
␈↓ ↓H␈↓A more elaborate example of recursive definition is given by
␈↓ ↓H␈↓␈↓ βm␈↓↓flatten[x] ← flat[x, ␈↓¬NIL␈↓↓] ␈↓
␈↓ ↓H␈↓8.8)
␈↓ ↓H␈↓␈↓ βd␈↓↓flat[x, u] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x.u ␈↓αelse␈↓↓ flat[␈↓αa|␈↓↓x, flat[␈↓αd|␈↓↓x, u]]␈↓.
␈↓ ↓H␈↓We have
␈↓ ↓H␈↓␈↓ β8␈↓↓flatten[␈↓¬((A.B).C)␈↓↓]␈↓␈↓ ¬_␈↓↓= flat[␈↓¬((A.B).C)␈↓↓, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= flat[␈↓¬(A.B)␈↓↓, flat[␈↓¬C, ␈↓↓␈↓¬NIL␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= flat[␈↓¬(A.B), (C)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= flat[␈↓¬A, ␈↓↓flat[␈↓¬B, (C)␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= flat[␈↓¬A, (B C)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬(A B C)␈↓↓␈↓.
␈↓ ↓H␈↓The␈αreader␈αwill␈α
see␈αthat␈αthe␈αvalue␈α
of␈α␈↓↓flatten[x]␈↓␈αis␈αa␈α
list␈αof␈αthe␈αatoms␈α
of␈αthe␈αS-expression␈α
␈↓↓x␈↓␈αfrom
␈↓ ↓H␈↓left to right. Thus ␈↓↓flatten[␈↓¬((A B) A)␈↓↓] = ␈↓¬(A B NIL A NIL)␈↓↓␈↓.
␈↓ ↓H␈↓ Many␈α⊃functions␈α⊃can␈α⊃be␈α∩conveniently␈α⊃written␈α⊃in␈α⊃more␈α∩than␈α⊃one␈α⊃way.␈α⊃ For␈α∩example,␈α⊃the
␈↓ ↓H␈↓function ␈↓↓reverse␈↓ mentioned above can be written without an auxiliary function as follows:
␈↓ ↓H␈↓8.9) ␈↓ ∧α␈↓↓reverse u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ reverse ␈↓αd|␈↓↓u * <␈↓αa|␈↓↓u>␈↓,
␈↓ ↓H␈↓but␈α⊃the␈α⊂earlier␈α⊃definition␈α⊂involves␈α⊃less␈α⊂computation,␈α⊃because␈α⊂*␈α⊃takes␈α⊂time␈α⊃proportional␈α⊃to␈α⊂the
␈↓ ↓H␈↓16␈↓ εβChapter I␈↓ H
␈↓ ↓H␈↓length␈αof␈αits␈αfirst␈αargument.␈α
Similarly␈αthe␈αfunction␈αcomputed␈αby␈α
␈↓↓flat␈↓␈αcan␈αalso␈αbe␈αcomputed␈αby␈α
the
␈↓ ↓H␈↓simpler but less efficient program ␈↓↓fringe␈↓
␈↓ ↓H␈↓8.10) ␈↓ βi␈↓↓fringe x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <x> ␈↓αelse␈↓↓ fringe ␈↓αa|␈↓↓x * fringe ␈↓αd|␈↓↓x␈↓,
␈↓ ↓H␈↓ The␈α∪use␈α∪of␈α∪conditional␈α∪expressions␈α∪for␈α∪recursive␈α∪function␈α∪definition␈α∪is␈α∪not␈α∀limited␈α∪to
␈↓ ↓H␈↓functions␈α
of␈α
S-expressions.␈α For␈α
example,␈α
the␈αfactorial␈α
function␈α
and␈αthe␈α
Euclidean␈α
algorithm␈αfor
␈↓ ↓H␈↓the greatest common divisor are expressed as follows:
␈↓ ↓H␈↓8.11) ␈↓ ¬α␈↓↓n! ← ␈↓αif␈↓↓ n=0 ␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ n(n-1)!␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓8.12) ␈↓ αV␈↓↓gcd(m, n) ← ␈↓αif␈↓↓ m>n ␈↓αthen␈↓↓ gcd(n, m) ␈↓αelse␈↓↓ ␈↓αif␈↓↓ m=0 ␈↓αthen␈↓↓ n ␈↓αelse␈↓↓ gcd(n mod m, m)␈↓
␈↓ ↓H␈↓where␈α⊂␈↓↓n␈α⊂mod␈α⊂m␈↓␈α⊂denotes␈α⊂the␈α⊂remainder␈α⊂when␈α⊂␈↓↓n␈↓␈α⊂is␈α⊂divided␈α⊂by␈α⊂␈↓↓m␈↓␈α⊂and␈α⊂may␈α⊂itself␈α⊂be␈α⊂expressed
␈↓ ↓H␈↓recursively by
␈↓ ↓H␈↓8.13)␈↓ ∧*␈↓↓n mod m ← ␈↓αif␈↓↓ n<m ␈↓αthen␈↓↓ n ␈↓αelse␈↓↓ (n-m) mod m␈↓.
␈↓ ↓H␈↓ The␈αinternal␈αform␈αof␈αfunction␈αdefinitions␈αdepends␈αon␈αthe␈αimplementation.␈α MACLISP␈αuses
␈↓ ↓H␈↓the form
␈↓ ↓H␈↓␈↓ α← (␈↓¬DEFUN ␈↓<function name> <list of variables> <right hand side>).
␈↓ ↓H␈↓Stanford␈αLISP␈αand␈αUCI␈αLISP␈αfor␈αthe␈αPDP-10␈αcomputer␈αuse␈αthe␈αsame␈αform␈αwith␈α␈↓¬DE␈α␈↓␈αin␈αplace␈αof
␈↓ ↓H␈↓␈↓¬DEFUN. ␈↓ Thus in MACLISP the definition of ␈↓↓subst␈↓ is
␈↓ ↓H␈↓¬␈↓ βx(DEFUN SUBST (X Y Z)
␈↓ ↓H␈↓¬␈↓ ∧_(COND ((ATOM Z) (COND ((EQ Z X) Y) (T Z)))
␈↓ ↓H␈↓¬␈↓ ∧x(T (CONS (SUBST X Y (CAR Z)) (SUBST X Y (CDR Z)))))),
␈↓ ↓H␈↓and the definition of ␈↓↓alt␈↓ is
␈↓ ↓H␈↓¬␈↓ βx(DEFUN ALT (U)
␈↓ ↓H␈↓¬␈↓ ∧_(COND ((OR (NULL U) (NULL (CDR U))) U)
␈↓ ↓H␈↓¬␈↓ ∧x(T (CONS (CAR U) (ALT (CDDR U)))))).
␈↓ ↓H␈↓Yet␈α
another␈α
notation␈α
for␈α
function␈α
definition␈αcalled␈α
the␈α
␈↓¬DEFPROP␈α
␈↓notation␈α
will␈α
be␈α
explained␈αafter
␈↓ ↓H␈↓λ-expressions have been introduced.
␈↓ ↓H␈↓α␈↓ ε
Exercises
␈↓ ↓H␈↓ 1. Consider the function ␈↓↓drop␈↓ defined by
␈↓ ↓H␈↓␈↓ ∧⊗␈↓↓drop[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ <␈↓αa|␈↓↓u> . drop[␈↓αd|␈↓↓u]␈↓.
␈↓ ↓H␈↓␈↓ εβChapter I␈↓ *17
␈↓ ↓H␈↓Compute␈α(by␈αhand)␈α␈↓↓drop[␈↓¬(A␈αB␈α
C)␈↓↓]␈↓.␈α What␈αdoes␈α␈↓↓drop␈↓␈αdo␈αto␈α
lists␈αin␈αgeneral?␈α Write␈α␈↓↓drop␈↓␈αin␈α
internal
␈↓ ↓H␈↓notation using ␈↓¬DEFUN. ␈↓
␈↓ ↓H␈↓ 2. What does the function
␈↓ ↓H␈↓␈↓ ∧∪␈↓↓r2[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ reverse[␈↓αa|␈↓↓u] . r2[␈↓αd|␈↓↓u]␈↓
␈↓ ↓H␈↓do to lists of lists? How about
␈↓ ↓H␈↓␈↓ ∧∞␈↓↓r3[x] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ reverse[r4[x]] ␈↓
␈↓ ↓H␈↓␈↓ ∧↔␈↓↓r4[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ r3[␈↓αa|␈↓↓u] . r4[␈↓αd|␈↓↓u]? ␈↓
␈↓ ↓H␈↓ 3. Compare
␈↓ ↓H␈↓␈↓ ∧ε␈↓↓r3'[x] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ <r3'[␈↓αd|␈↓↓x]> * <r3'[␈↓αa|␈↓↓x]>␈↓
␈↓ ↓H␈↓with the function ␈↓↓r3␈↓ of the preceding example.
␈↓ ↓H␈↓ 4. Consider ␈↓↓r5␈↓ defined by
␈↓ ↓H␈↓␈↓ β∪␈↓↓r5[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ [␈↓αa|␈↓↓r5[␈↓αd|␈↓↓u]] . r5[␈↓αa|␈↓↓u . r5[␈↓αd|␈↓↓r5[␈↓αd|␈↓↓u]]]␈↓.
␈↓ ↓H␈↓Compute␈α␈↓↓r5[␈↓¬(A␈αB␈αC␈αD)␈↓↓]␈↓.␈α What␈αdoes␈α␈↓↓r5␈↓␈αdo?␈α Needless␈αto␈αsay,␈αthis␈αis␈αnot␈αa␈αgood␈αway␈αof␈αcomputing
␈↓ ↓H␈↓this␈α⊂function␈α⊃even␈α⊂though␈α⊂it␈α⊃involves␈α⊂no␈α⊂auxiliary␈α⊃functions.␈α⊂ [This␈α⊂ingeneous␈α⊃definition␈α⊂was
␈↓ ↓H␈↓discovered by S. Ness]
␈↓ ↓H␈↓9. ␈↓αLambda expressions and functions with functions as arguments.␈↓
␈↓ ↓H␈↓ It␈αis␈αcommon␈αto␈α
use␈αphrases␈αlike␈α"the␈α
function␈α␈↓↓2x+y␈↓".␈α This␈αis␈α
not␈αa␈αprecise␈αnotation␈α
because
␈↓ ↓H␈↓we␈αcannot␈αsay␈α␈↓↓[2x+y][3, 4]␈↓␈αand␈αknow␈αwhether␈α
the␈αdesired␈αresult␈αis␈α2␈↓π*␈↓3+4␈αor␈α2␈↓π*␈↓4+3␈α
regarding␈αthe
␈↓ ↓H␈↓expression␈α⊃as␈α⊃a␈α⊃function␈α⊃of␈α⊃two␈α⊃variables.␈α⊃ Worse␈α⊃yet,␈α⊃we␈α⊃might␈α⊃have␈α⊃meant␈α∩a␈α⊃one-variable
␈↓ ↓H␈↓function of ␈↓↓x␈↓ wherein ␈↓↓y␈↓ is regarded as a parameter.
␈↓ ↓H␈↓ The␈αproblem␈αof␈αgiving␈αnames␈αto␈αfunctions␈αis␈αsolved␈αby␈αChurch's␈αλ-notation.␈α In␈αthe␈αabove
␈↓ ↓H␈↓example,␈αwe␈αwould␈αwrite␈α␈↓↓λx y: 2x+y␈↓␈αto␈αdenote␈αthe␈αfunction␈αof␈αtwo␈αvariables␈αwith␈αfirst␈αargument␈α␈↓↓x␈↓
␈↓ ↓H␈↓and␈α#second␈α"argument␈α#␈↓↓y␈↓␈α"whose␈α#value␈α#is␈α"given␈α#by␈α"the␈α#expression␈α#␈↓↓2x+y␈↓.␈α" Thus,
␈↓ ↓H␈↓␈↓↓[λx y: 2x+y][3, 4] = 10␈↓ and ␈↓↓[λy x: 2x+y][3, 4]= 11.␈↓
␈↓ ↓H␈↓ Like␈α⊂variables␈α⊃of␈α⊂integration␈α⊂and␈α⊃the␈α⊂bound␈α⊂variables␈α⊃of␈α⊂quantifiers␈α⊂in␈α⊃logic,␈α⊂variables
␈↓ ↓H␈↓following␈α∪the␈α∪ λ ␈α∪are␈α∀bound␈α∪or␈α∪dummy␈α∪and␈α∪may␈α∀be␈α∪replaced␈α∪by␈α∪any␈α∪others␈α∀provided␈α∪the
␈↓ ↓H␈↓replacement␈α
is␈α
done␈α
consistently␈α
throughout␈α
the␈α
expression␈α
and␈α
does␈α
not␈α
make␈α
any␈αvariable␈α
bound
␈↓ ↓H␈↓by␈α
λ ␈α
the␈α
same␈α
as␈α
a␈α
free␈α
variable␈α
in␈α
the␈α
expression.␈α
Thus␈α
␈↓↓λx y: 2x+y␈↓␈α
represents␈α
the␈α
same␈α
function
␈↓ ↓H␈↓as␈α␈↓↓λy x: 2y+x␈↓␈αor␈α␈↓↓λu v: 2u+v␈↓␈α,␈αbut␈αin␈αthe␈αfunction␈αof␈αone␈αargument␈α␈↓↓λx: 2x+y␈↓,␈αwe␈αcannot␈αreplace␈αthe
␈↓ ↓H␈↓variable ␈↓↓x␈↓ by ␈↓↓y,␈↓ though we could replace it by ␈↓↓u.␈↓
␈↓ ↓H␈↓ λ-notation␈α∞plays␈α
two␈α∞important␈α
roles␈α∞in␈α
LISP.␈α∞ First,␈α
it␈α∞allows␈α
us␈α∞to␈α
rewrite␈α∞an␈α
expression
␈↓ ↓H␈↓18␈↓ εβChapter I␈↓ H
␈↓ ↓H␈↓containing␈αtwo␈αor␈αmore␈αoccurrences␈αof␈αthe␈αsame␈αsub-expression␈αin␈αsuch␈αa␈αway␈αthat␈αthe␈αexpression
␈↓ ↓H␈↓occurs␈α∪only␈α∩once.␈α∪Thus␈α∩␈↓↓(2x+1)␈↓∧4␈↓↓+3(2x+1)␈↓∧3␈↓␈α∪can␈α∩be␈α∪written␈α∩␈↓↓[λw: w␈↓∧4␈↓↓+3w␈↓∧3␈↓↓][2x+1]␈↓.␈α∪ This␈α∪can␈α∩save
␈↓ ↓H␈↓considerable␈αcomputation,␈α
and␈αcorresponds␈αto␈α
the␈αpractice␈αin␈α
sequential␈αprogramming␈αof␈α
assigning
␈↓ ↓H␈↓to␈αa␈α
variable␈αthe␈αvalue␈α
of␈αa␈αsub-expression␈α
that␈αoccurs␈αmore␈α
than␈αonce␈αin␈α
an␈αexpression␈αand␈α
then
␈↓ ↓H␈↓writing the expression in terms of the variable. It is sometimes referred to as λ-binding.
␈↓ ↓H␈↓ The␈α∂second␈α∂use␈α∂of␈α⊂λ-expressions␈α∂is␈α∂in␈α∂using␈α⊂functions␈α∂that␈α∂take␈α∂functions␈α⊂as␈α∂arguments.
␈↓ ↓H␈↓Suppose␈αwe␈αwant␈αto␈αform␈αa␈αnew␈αlist␈αfrom␈αan␈αold␈αone␈αby␈αapplying␈αa␈αfunction␈α␈↓↓f␈↓␈αto␈αeach␈αelement␈αof
␈↓ ↓H␈↓the list. This can be done using the function ␈↓↓mapcar␈↓ defined by
␈↓ ↓H␈↓9.1) ␈↓ βL␈↓↓mapcar[f, u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ f[␈↓αa|␈↓↓u] . mapcar[f, ␈↓αd|␈↓↓u]␈↓.
␈↓ ↓H␈↓Such␈α∂a␈α∂function␈α∂is␈α∂called␈α∂a␈α∂␈↓↓functional.␈↓␈α∂ It␈α∂can␈α∂be␈α∂viewed␈α∂as␈α∂mapping␈α∂a␈α∂function␈α∂into␈α∞another
␈↓ ↓H␈↓function.␈α Suppose␈αthe␈αoperation␈αwe␈αwant␈αto␈αperform␈αis␈αsquaring,␈αand␈αwe␈αwant␈αto␈αapply␈αit␈αto␈αthe
␈↓ ↓H␈↓list ␈↓¬(1 2 3 4 5 6 7)␈↓. We have
␈↓ ↓H␈↓␈↓ βi␈↓↓mapcar[λx: x␈↓∧2␈↓↓, ␈↓¬(1 2 3 4 5 6 7)␈↓↓] = ␈↓¬(1 4 9 16 25 36 49)␈↓↓␈↓.
␈↓ ↓H␈↓ [Some␈α∞implementations␈α∞of␈α∞LISP␈α∞allow␈α∞mapping␈α∞functions␈α∞to␈α∞take␈α∞an␈α∞arbitrary␈α∞number␈α∞of
␈↓ ↓H␈↓lists␈α
as␈α
arguments.␈α
The␈α∞number␈α
of␈α
lists␈α
is␈α
the␈α∞number␈α
of␈α
arguments␈α
expected␈α
by␈α∞the␈α
functional
␈↓ ↓H␈↓argument␈αand␈αthe␈αmapping␈αterminates␈αwhen␈αthe␈αshortest␈αlist␈αis␈αexhausted.␈α Some␈αimplementations
␈↓ ↓H␈↓of LISP require the arguments in reverse order - functional argument second.]
␈↓ ↓H␈↓ A␈αmore␈αgenerally␈αuseful␈αoperation␈αthan␈α␈↓↓mapcar␈↓␈αis␈α␈↓↓maplist␈↓␈αin␈αwhich␈αthe␈αfunction␈αis␈αapplied
␈↓ ↓H␈↓to the successive sublists of the list rather than to the elements. ␈↓↓maplist␈↓ is defined by
␈↓ ↓H␈↓9.2) ␈↓ βT␈↓↓maplist[f, u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ f[u] . maplist[f, ␈↓αd|␈↓↓u]␈↓.
␈↓ ↓H␈↓ As␈α⊃an␈α⊃application␈α⊂of␈α⊃␈↓↓maplist␈↓␈α⊃and␈α⊂functional␈α⊃arguments,␈α⊃we␈α⊂shall␈α⊃define␈α⊃a␈α⊃function␈α⊂for
␈↓ ↓H␈↓differentiating␈αalgebraic␈α
expressions␈αinvolving␈αsums␈α
and␈αproducts.␈α The␈α
expressions␈αare␈α
built␈αup
␈↓ ↓H␈↓from atoms denoting variables and integer constants according to the syntax
␈↓ ↓H␈↓ <expression> ::= <variable> | <integer> | (␈↓¬PLUS ␈↓<explist>) | (␈↓¬TIMES ␈↓<explist>)
␈↓ ↓H␈↓9.3)
␈↓ ↓H␈↓ <explist> ::= <expression> | <expression><explist>
␈↓ ↓H␈↓Here,␈α∩␈↓¬PLUS␈α∩␈↓followed␈α⊃by␈α∩a␈α∩list␈α⊃of␈α∩arguments␈α∩denotes␈α⊃the␈α∩sum␈α∩of␈α⊃these␈α∩arguments␈α∩and␈α⊃␈↓¬TIMES
␈↓ ↓H␈↓¬␈↓followed␈α
by␈α
a␈α∞list␈α
of␈α
arguments␈α∞denotes␈α
their␈α
product.␈α
The␈α∞function␈α
␈↓↓diff[e, v]␈↓␈α
gives␈α∞the␈α
partial
␈↓ ↓H␈↓derivative of the expression ␈↓↓e␈↓ with respect to the variable ␈↓↓v.␈↓ We have
␈↓ ↓H␈↓␈↓ εβChapter I␈↓ *19
␈↓ ↓H␈↓␈↓ αx␈↓↓diff[e, v] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ [␈↓αif␈↓↓ e = v ␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ 0]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬PLUS␈↓↓ ␈↓αthen␈↓↓ ␈↓¬PLUS␈↓↓ . mapcar[[λx: diff[x, v]], ␈↓αd|␈↓↓e]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬TIMES␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓9.4)␈↓ βX␈↓↓␈↓¬PLUS␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓. maplist[␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓[λx: ␈↓¬TIMES␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓↓. maplist[␈↓
␈↓ ↓H␈↓␈↓ ¬λ␈↓↓[λy: ␈↓αif␈↓↓ x = y ␈↓αthen␈↓↓ diff[␈↓αa|␈↓↓y, v] ␈↓αelse␈↓↓ ␈↓αa|␈↓↓y], ␈↓αd|␈↓↓e]], ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αd|␈↓↓e]␈↓
␈↓ ↓H␈↓The term that describes the rule for differentiating products corresponds to the rule
␈↓ ↓H␈↓␈↓ ∧&␈↓↓∂/∂v[␈↓πP␈↓↓␈↓βi␈↓↓ e␈↓βi␈↓↓] = ␈↓πS␈↓↓␈↓βi␈↓↓␈↓π P␈↓↓␈↓βj␈↓↓ [␈↓αif␈↓↓ i=j ␈↓αthen␈↓↓ ∂e␈↓βj␈↓↓/∂v ␈↓αelse␈↓↓ e␈↓βj␈↓↓] .␈↓
␈↓ ↓H␈↓and␈α∂␈↓↓maplist␈↓␈α⊂has␈α∂to␈α⊂be␈α∂used␈α⊂rather␈α∂than␈α⊂␈↓↓mapcar␈↓␈α∂since␈α⊂whether␈α∂to␈α⊂differentiate␈α∂in␈α⊂forming␈α∂the
␈↓ ↓H␈↓product is determined by equality of the indices ␈↓↓i␈↓ and ␈↓↓j␈↓ rather than equality of the terms ␈↓↓e␈↓βi␈↓ and ␈↓↓e␈↓βj␈↓.
␈↓ ↓H␈↓ The internal form for a λ-expression is
␈↓ ↓H␈↓␈↓ βN(␈↓¬LAMBDA ␈↓<list of variables> <expression to be evaluated>).
␈↓ ↓H␈↓Thus␈α
␈↓↓λx:␈α
diff[x,v]␈↓␈α
is␈αwritten␈α
␈↓¬(LAMBDA␈α
(X)␈α
(DIFF␈αX␈α
V))␈↓.␈α
The␈α
internal␈α
form␈αof␈α
of␈α
␈↓↓diff␈↓␈α
is␈αgiven␈α
below.
␈↓ ↓H␈↓Notice that the function arguments to ␈↓↓maplist␈↓ and ␈↓↓mapcar␈↓ have the form
␈↓ ↓H␈↓␈↓ ¬∩(␈↓¬FUNCTION ␈↓ <λ-expression>).
␈↓ ↓H␈↓This␈αis␈α
necessary␈αif␈α
the␈αdefinition␈α
is␈αto␈α
be␈αcompiled␈α
as␈αthe␈α
compiler␈αmust␈α
recognize␈αthe␈α
fact␈αthat
␈↓ ↓H␈↓the␈αfollowing␈α
code␈αmust␈α
be␈αcompiled␈αas␈α
a␈αfunction.␈α
If␈αthe␈αdefinition␈α
is␈αonly␈α
to␈αbe␈αinterpreted␈α
then
␈↓ ↓H␈↓␈↓¬FUNCTION␈α
␈↓␈α
has␈α
the␈α
same␈α
effect␈α
as␈α
␈↓¬QUOTE␈α∞␈↓and␈α
in␈α
fact␈α
we␈α
will␈α
often␈α
use␈α
␈↓¬QUOTE␈α
␈↓in␈α∞giving␈α
internal
␈↓ ↓H␈↓form definitions assuming that they will be interpreted rather than compiled.
␈↓ ↓H␈↓¬␈↓ α_(DEFUN DIFF (E V)
␈↓ ↓H␈↓¬␈↓ α8(COND ((ATOM E) (COND ((EQ E V) 1) (T 0)))
␈↓ ↓H␈↓¬␈↓ β_((EQ (CAR E) (QUOTE PLUS))
␈↓ ↓H␈↓¬␈↓ βH(CONS (QUOTE PLUS)
␈↓ ↓H␈↓¬␈↓ ∧((MAPCAR (FUNCTION (LAMBDA (X) (DIFF X V))) (CDR E))))
␈↓ ↓H␈↓¬␈↓ β_((EQ (CAR E) (QUOTE TIMES))
␈↓ ↓H␈↓¬␈↓ βH(CONS (QUOTE PLUS)
␈↓ ↓H␈↓¬␈↓ ∧((MAPLIST (FUNCTION (LAMBDA (X)
␈↓ ↓H␈↓¬␈↓ ¬X(CONS (QUOTE TIMES)
␈↓ ↓H␈↓¬␈↓ ε8(MAPLIST (FUNCTION (LAMBDA (Y)
␈↓ ↓H␈↓¬␈↓ πh(COND ((EQ X Y) (DIFF (CAR Y) V))
␈↓ ↓H␈↓¬␈↓ λH(T (CAR Y)))))
␈↓ ↓H␈↓¬␈↓ πH(CDR E)))))
␈↓ ↓H␈↓¬␈↓ ¬8(CDR E)))))).
␈↓ ↓H␈↓20␈↓ εβChapter I␈↓ H
␈↓ ↓H␈↓ The␈α
above␈α
paragraphing␈α
(known␈α
as␈α"pretty␈α
printing")␈α
makes␈α
function␈α
definitions␈α
easier␈αto
␈↓ ↓H␈↓read because items beginning in the same column are at the same parenthetical level.
␈↓ ↓H␈↓ Two␈α∩additional␈α⊃useful␈α∩functions␈α⊃with␈α∩functions␈α⊃as␈α∩arguments␈α⊃are␈α∩the␈α⊃predicates ␈↓↓andlis␈↓
␈↓ ↓H␈↓ and ␈↓↓orlis␈↓ defined by the equations
␈↓ ↓H␈↓9.5) ␈↓ ∧#␈↓↓andlis[p, u] ← ␈↓αn|␈↓↓u ∨ [p[␈↓αa|␈↓↓u] ∧ andlis[p, ␈↓αd|␈↓↓u]]␈↓
␈↓ ↓H␈↓9.6) ␈↓ ∧)␈↓↓ orlis[p, u] ← ¬␈↓αn|␈↓↓u ∧ [p[␈↓αa|␈↓↓u] ∨ orlis[p, ␈↓αd|␈↓↓u]]␈↓.
␈↓ ↓H␈↓ Another␈α∞way␈α∞of␈α∞writing␈α∞function␈α∞definitions␈α∂in␈α∞internal␈α∞notation␈α∞uses␈α∞␈↓¬LAMBDA␈α∞␈↓to␈α∂make␈α∞a
␈↓ ↓H␈↓function of the right side of a definition. It is like writing ␈↓↓subst␈↓ and ␈↓↓alt␈↓ as
␈↓ ↓H␈↓␈↓ α∪␈↓↓subst ← λx y z:[␈↓αif␈↓↓ ␈↓αat|␈↓↓z ␈↓αthen␈↓↓ [␈↓αif␈↓↓ z ␈↓αeq␈↓↓ y ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ z] ␈↓αelse␈↓↓ subst[x,y,␈↓αa|␈↓↓z] . subst[x,y,␈↓αd|␈↓↓z]]␈↓
␈↓ ↓H␈↓␈↓ ∧π␈↓↓alt ← λu.[␈↓αif␈↓↓ ␈↓αn|␈↓↓u ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . alt ␈↓αdd|␈↓↓u]␈↓.
␈↓ ↓H␈↓Internally these definitions of ␈↓↓subst␈↓ and ␈↓↓alt␈↓ take the forms
␈↓ ↓H␈↓¬␈↓ β8(DEFPROP SUBST
␈↓ ↓H␈↓¬␈↓ βX(LAMBDA (X Y Z)
␈↓ ↓H␈↓¬␈↓ βx(COND ((ATOM Z) (COND ((EQ Z X) Y) (T Z)))
␈↓ ↓H␈↓¬␈↓ ∧X(T (CONS (SUBST X Y (CAR Z)) (SUBST X Y (CDR Z)))))) EXPR)
␈↓ ↓H␈↓¬␈↓ β8(DEFPROP ALT
␈↓ ↓H␈↓¬␈↓ βX(LAMBDA (U) (COND ((OR (NULL U) (NULL (CDR U))) U)
␈↓ ↓H␈↓¬␈↓ ¬x(T (CONS (CAR U) (ALT (CDDR U)))))) EXPR).
␈↓ ↓H␈↓ The general form for this manner of writing functon definitions is
␈↓ ↓H␈↓␈↓ βI(␈↓¬DEFPROP ␈↓<function name> <defining λ-expression> ␈↓¬EXPR) ␈↓
␈↓ ↓H␈↓and␈α⊂it␈α⊃is␈α⊂often␈α⊃used␈α⊂by␈α⊃programs␈α⊂that␈α⊃output␈α⊂LISP.␈α⊂ It␈α⊃is␈α⊂a␈α⊃special␈α⊂case␈α⊃of␈α⊂an␈α⊃operation␈α⊂on
␈↓ ↓H␈↓property␈α
lists.␈α
It␈αputs␈α
the␈α
λ-expression␈αon␈α
the␈α
␈↓¬EXPR␈α␈↓property␈α
of␈α
the␈αfunction␈α
name␈α
(which␈α
is␈αan
␈↓ ↓H␈↓atom).␈α
␈↓¬EXPR␈α␈↓says␈α
that␈αthe␈α
item␈α
is␈αa␈α
LISP␈αfunction␈α
defined␈α
by␈αan␈α
S-expression.␈α (Rather␈α
than␈αby␈α
a
␈↓ ↓H␈↓machine␈αlanguage␈αsubroutine,␈αfor␈αinstance).␈α The␈α␈↓¬DEFUN␈α␈↓form␈αof␈αfunction␈αdefinition␈αhas␈αthe␈αsame
␈↓ ↓H␈↓effect␈αin␈αthat␈α
it␈αforms␈αa␈αλ-expression␈α
out␈αof␈αthe␈αlist␈α
of␈αvariables␈αand␈αthe␈α
right␈αhand␈αside␈αand␈α
puts
␈↓ ↓H␈↓it on the ␈↓¬EXPR ␈↓property of the function name.
␈↓ ↓H␈↓␈↓ εβChapter I␈↓ *21
␈↓ ↓H␈↓α␈↓ ε
Exercises
␈↓ ↓H␈↓ 1.␈αCompute␈α␈↓↓diff[␈↓¬(TIMES␈αX␈α(PLUS␈αY␈α1)␈α3),␈αX␈↓↓]␈↓␈αusing␈αthe␈αabove␈αdefinition␈αof␈α␈↓↓diff.␈↓␈α Now␈αdo␈α
you
␈↓ ↓H␈↓see why algebraic simplification is important?
␈↓ ↓H␈↓ 2. Compute ␈↓↓orlis[␈↓αat␈↓↓, ␈↓¬((A B) (C D) E)␈↓↓]␈↓.
␈↓ ↓H␈↓10. ␈↓αLabel.␈↓
␈↓ ↓H␈↓ The␈αλ␈α
mechanism␈αis␈α
not␈αadequate␈α
for␈αproviding␈α
names␈αfor␈α
recursive␈αfunctions,␈α
because␈αin
␈↓ ↓H␈↓this␈αcase␈αthere␈αhas␈αto␈αbe␈αa␈αway␈αof␈αreferring␈αto␈αthe␈αfunction␈αname␈αwithin␈αthe␈αfunction.␈α Therefore,
␈↓ ↓H␈↓we␈α∞use␈α∞the␈α∞notation ␈↓↓label[f, e]␈↓ to␈α∞denote␈α∞the␈α∞expression␈α∞␈↓↓e␈↓␈α∞but␈α∞where␈α∞occurrences␈α∞of␈α∞␈↓↓f␈↓␈α∂within␈α∞␈↓↓e␈↓
␈↓ ↓H␈↓refer␈α∞to␈α∞the␈α
whole␈α∞expression.␈α∞ For␈α∞example,␈α
suppose␈α∞we␈α∞wished␈α∞to␈α
define␈α∞a␈α∞function␈α∞that␈α
takes
␈↓ ↓H␈↓alternate elements of each element of a list and makes a list of these. Thus, we want
␈↓ ↓H␈↓␈↓ βy␈↓↓glub[␈↓¬((A B C) (A B C D) (X Y Z))␈↓↓] = ␈↓¬((A C) (A C) (X Z))␈↓↓␈↓.
␈↓ ↓H␈↓We can make the definition
␈↓ ↓H␈↓10.1) ␈↓ αZ␈↓↓glub[u] ← mapcar[label[alt, λu: ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ∨ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . alt[␈↓αdd|␈↓↓u]], u]␈↓.
␈↓ ↓H␈↓in internal form this would be written
␈↓ ↓H␈↓¬␈↓ β8(DEFUN GLUB (X)
␈↓ ↓H␈↓¬␈↓ βX(MAPCAR (QUOTE (LABEL ALT (LAMBDA (X)
␈↓ ↓H␈↓¬␈↓ ¬x(COND (OR (NULL X) (NULL (CDR X))) X)
␈↓ ↓H␈↓¬␈↓ εX(T (CONS (CAR X) (ALT (CDDR X)))))))
␈↓ ↓H␈↓¬␈↓ ∧XX)).
␈↓ ↓H␈↓ The internal form of the label construct is
␈↓ ↓H␈↓␈↓ ∧J(␈↓¬LABEL ␈↓<name> <function expression>),
␈↓ ↓H␈↓ The␈α
identifier␈α
␈↓↓alt␈↓␈α
in␈α
the␈α
above␈α
example␈α
is␈α
bound␈α
by␈α
␈↓↓label␈↓␈α
and␈α
is␈α
local␈α
to␈α∞that␈α
expression,
␈↓ ↓H␈↓and␈α
this␈α
is␈α
the␈α
general␈α
rule.␈α
The␈α
label␈α
construct␈αis␈α
not␈α
often␈α
used␈α
in␈α
LISP␈α
since␈α
it␈α
is␈α
more␈αusual␈α
to
␈↓ ↓H␈↓give functions global definitions.
␈↓ ↓H␈↓11. ␈↓αNumerical computation.␈↓
␈↓ ↓H␈↓ Numerical␈α
calculation␈α
and␈α
symbolic␈α
calculation␈α
must␈α
often␈α
be␈α
combined,␈α
so␈α
LISP␈αprovides
␈↓ ↓H␈↓for␈α⊃numerical␈α∩computation␈α⊃also.␈α∩ In␈α⊃the␈α∩first␈α⊃place,␈α∩we␈α⊃need␈α∩to␈α⊃include␈α∩numbers␈α⊃as␈α∩parts␈α⊃of
␈↓ ↓H␈↓symbolic␈α
expressions.␈α
LISP␈αhas␈α
both␈α
integer␈αand␈α
floating␈α
point␈αnumbers␈α
which␈α
are␈α
regarded␈αas
␈↓ ↓H␈↓atoms.␈α
These␈α∞numbers␈α
may␈α
be␈α∞included␈α
as␈α
atoms␈α∞in␈α
writing␈α
S-expressions.␈α∞ Thus␈α
we␈α∞can␈α
have
␈↓ ↓H␈↓the lists:
␈↓ ↓H␈↓22␈↓ εβChapter I␈↓ H
␈↓ ↓H␈↓¬␈↓ ¬_(1 3 5)
␈↓ ↓H␈↓¬␈↓ ¬_(3.5 6.1 -7.2E9)
␈↓ ↓H␈↓¬␈↓ ¬_(PLUS X 1.3).
␈↓ ↓H␈↓The␈αfirst␈αis␈αa␈αlist␈αof␈α
integers,␈αthe␈αsecond␈αa␈αlist␈αof␈α
floating␈αpoint␈αnumbers,␈αand␈αthe␈αthird␈αa␈α
symbolic
␈↓ ↓H␈↓list␈α∂containing␈α∂both␈α∂numerical␈α∂and␈α∂non-numerical␈α∂atoms.␈α∂ Integers␈α∂are␈α∂written␈α⊂without␈α∂decimal
␈↓ ↓H␈↓points␈αwhich␈αare␈α
used␈αto␈αsignal␈α
floating␈αpoint␈αnumbers.␈α As␈α
in␈αFORTRAN,␈αthe␈α
letter␈αE␈αis␈αused␈α
to
␈↓ ↓H␈↓signal␈αthe␈αexponent␈αof␈αa␈αfloating␈αpoint␈αnumber␈αwhich␈αis␈αa␈αsigned␈αinteger.␈α The␈αsizes␈α
of␈αnumbers
␈↓ ↓H␈↓admitted␈α
depends␈α
on␈α
the␈α
implementation.␈α
When␈α
a␈α
dotted␈α
pair,␈α
say␈α
␈↓¬(1␈α
.␈α
2)␈↓␈α
is␈α
wanted,␈α∞the␈α
spaces
␈↓ ↓H␈↓around␈α
the␈α
dot␈αdistinguish␈α
it␈α
from␈αthe␈α
list␈α
␈↓¬(1.2)␈↓␈αwhose␈α
sole␈α
element␈αis␈α
the␈α
floating␈α
point␈αnumber
␈↓ ↓H␈↓1.2.
␈↓ ↓H␈↓ In␈α∞external␈α∞language␈α∞we␈α∞will␈α∞use␈α∞ordinary␈α∞mathematical␈α∞notation␈α∞for␈α∂numerical␈α∞functions.
␈↓ ↓H␈↓For␈αexponentiation␈αwe␈αwill␈αuse␈αthe␈αusual␈αsuperscript␈αnotation␈α␈↓↓x␈↓∧y␈↓␈αwhen␈αtypographically␈αconvenient
␈↓ ↓H␈↓and␈α⊂the␈α⊂linear␈α⊂ALGOL␈α⊃notation␈α⊂␈↓↓x↑y␈↓␈α⊂when␈α⊂it␈α⊂isn't.␈α⊃ As␈α⊂an␈α⊂example␈α⊂of␈α⊂a␈α⊃function␈α⊂combining
␈↓ ↓H␈↓numeric and symbolic calculation we have the function giving the length of a list defined by
␈↓ ↓H␈↓11.1)␈↓ ∧.␈↓↓length u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ 0 ␈↓αelse␈↓↓ 1 + length ␈↓αd|␈↓↓u␈↓.
␈↓ ↓H␈↓The internal notation for numerical functions is shown in the following examples:
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(PLUS X Y ... Z)␈↓ for ␈↓↓x+y+...+z␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(TIMES X ... Z)␈↓ for ␈↓↓xy...z␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(MINUS X)␈↓ for ␈↓↓-x␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(DIFFERENCE X Y)␈↓ for ␈↓↓x-y␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(QUOTIENT X Y)␈↓ for ␈↓↓x/y␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(POWER X Y)␈↓ for ␈↓↓x␈↓∧y␈↓.
␈↓ ↓H␈↓ Besides␈α
functions␈αof␈α
numbers␈α
we␈αneed␈α
predicates␈α
on␈αnumbers␈α
and␈α
the␈αusual␈α
=,␈α
<,␈α>,␈α
≤,␈αand␈α
≥
␈↓ ↓H␈↓are␈α_used␈α_with␈α→the␈α_internal␈α_names␈α_␈↓¬EQUAL,␈α→␈↓␈↓¬LESSP,␈α_␈↓␈↓¬GREATERP,␈α_␈↓␈↓¬LESSEQP,␈α→␈↓and␈α_␈↓¬GREATEREQP,
␈↓ ↓H␈↓¬␈↓respectively.␈α
Not␈αall␈α
are␈α
implemented␈αin␈α
all␈αLISP␈α
systems,␈α
but␈αof␈α
course␈αthe␈α
remaining␈α
ones␈αcan
␈↓ ↓H␈↓be defined. An additional predicate, ␈↓↓numberp,␈↓ is used to distinguish numbers from other atoms.
␈↓ ↓H␈↓ Since␈α∂numbers␈α∂that␈α∂form␈α∂part␈α∂of␈α∂list␈α∂structures␈α∂must␈α∂be␈α∂represented␈α∂by␈α∂pointers␈α∂anyway,
␈↓ ↓H␈↓there␈α⊃is␈α∩room␈α⊃for␈α∩a␈α⊃flag␈α∩distinguishing␈α⊃floating␈α∩point␈α⊃numbers␈α∩and␈α⊃integers.␈α∩ Therefore,␈α⊃the
␈↓ ↓H␈↓arithmetic␈α⊃operations␈α⊂are␈α⊃programmed␈α⊂to␈α⊃treat␈α⊂types␈α⊃dynamically,␈α⊂i.e.␈α⊃a␈α⊂variable␈α⊃may␈α⊃take␈α⊂an
␈↓ ↓H␈↓integer␈αvalue␈αat␈α
one␈αstep␈αof␈αcomputation␈α
and␈αa␈αreal␈αvalue␈α
at␈αanother.␈α The␈α
subroutines␈αrealizing
␈↓ ↓H␈↓the arithmetic functions make the appropriate tests and create results of appropriate types.
␈↓ ↓H␈↓ It␈α_is␈α→worth␈α_remarking␈α→that␈α_including␈α_type␈α→flags␈α_in␈α→numbers␈α_would␈α→benefit␈α_many
␈↓ ↓H␈↓programming languages besides LISP and would not cost much in either storage or hardware.
␈↓ ↓H␈↓ Dynamic␈α⊂typing␈α⊂of␈α∂variables␈α⊂is␈α⊂slow␈α∂compared␈α⊂to␈α⊂direct␈α∂use␈α⊂of␈α⊂the␈α⊂machine's␈α∂arithmetic
␈↓ ↓H␈↓instructions,␈α∀so␈α∀that␈α∀LISP␈α∪can␈α∀be␈α∀efficiently␈α∀used␈α∪interpretatively␈α∀only␈α∀when␈α∀the␈α∪numerical
␈↓ ↓H␈↓calculations␈α
are␈αsmall␈α
or␈α
at␈αleast␈α
small␈αcompared␈α
to␈α
the␈αsymbolic␈α
calculations␈αin␈α
a␈α
problem.␈α The
␈↓ ↓H␈↓MACLSP␈α∪compiler␈α∩NCOMPLR␈α∪is␈α∩able␈α∪to␈α∩generate␈α∪efficient␈α∩arithmetic␈α∪code.␈α∪ In␈α∩particular,
␈↓ ↓H␈↓variables␈αcan␈α
be␈αdeclared␈αto␈α
be␈αof␈αa␈α
fixed␈αnumerical␈α
type␈αand␈αthe␈α
correct␈αmachine␈αinstructions␈α
are
␈↓ ↓H␈↓␈↓ εβChapter I␈↓ *23
␈↓ ↓H␈↓then␈α⊂generated␈α⊃so␈α⊂that␈α⊃runtime␈α⊂testing␈α⊂is␈α⊃not␈α⊂necessary.␈α⊃ Also,␈α⊂it␈α⊂uses␈α⊃separate␈α⊂stacks␈α⊃to␈α⊂store
␈↓ ↓H␈↓numerical␈αresults␈αso␈αthat␈αunecessary␈αconversion␈αfrom␈αthe␈αLISP␈αrepresentation␈αof␈αa␈αnumber␈αto␈αthe
␈↓ ↓H␈↓machine␈αrepresentation␈αcan␈αbe␈αavoided.␈α This␈αsaves␈αboth␈αtime␈αand␈αspace␈αas␈αeach␈αconversion␈αfrom
␈↓ ↓H␈↓an machine number to a LISP number requires a ␈↓↓cons␈↓ operation.
␈↓ ↓H␈↓ LISP␈α
can␈α
also␈α
deal␈α
with␈α
integers␈α
too␈α
large␈α
to␈α
be␈α
represented␈α
as␈α
a␈α
single␈α
machine␈α
word.␈α
Such
␈↓ ↓H␈↓numbers␈α
are␈α∞called␈α
"bignums"␈α∞and␈α
are␈α∞repsented␈α
in␈α
LISP␈α∞by␈α
a␈α∞pointer␈α
to␈α∞a␈α
list␈α∞structure␈α
which
␈↓ ↓H␈↓contains␈αthe␈αsign,␈α
a␈αflag␈αsaying␈αthat␈α
this␈αa␈α"bignum",␈α
and␈αa␈αlist␈αof␈α
the␈αnumbers␈αcorresponding␈αto␈α
a
␈↓ ↓H␈↓base␈α∪B␈α∩representation␈α∪of␈α∩the␈α∪number␈α∩for␈α∪some␈α∩suitable␈α∪B␈α∩(depending␈α∪on␈α∩the␈α∪machine␈α∩and
␈↓ ↓H␈↓implementation).
␈↓ ↓H␈↓ As␈αanother␈α
example␈αof␈α
a␈αcombined␈α
numeric␈αand␈α
symbolic␈αcomputation,␈α
here␈αis␈αan␈α
evaluator
␈↓ ↓H␈↓for␈αexpressions␈αwith␈αsums␈αand␈αproducts.␈α The␈αsyntax␈αfor␈αsuch␈αexpressions␈αwas␈αgiven␈αin␈α(9.3).␈α In
␈↓ ↓H␈↓order␈αto␈αevaluate␈α
an␈αexpression␈αwe␈α
need␈αa␈αway␈αof␈α
giving␈αvalues␈αto␈α
the␈αvariables␈αoccuring␈α
in␈αthe
␈↓ ↓H␈↓expression.␈α We␈α
will␈αuse␈αan␈α
␈↓↓association␈↓␈α␈↓↓list␈↓␈αfor␈α
this␈αpurpose.␈α An␈α
association␈αlist␈α(a-list)␈α
is␈αa␈αlist␈α
of
␈↓ ↓H␈↓pairs,␈αin␈αour␈αcase␈αthe␈αfirst␈αelement␈αof␈αeach␈αpair␈αis␈αa␈αvariable␈αand␈αthe␈αsecond␈αelement␈αis␈αthe␈αvalue
␈↓ ↓H␈↓associated␈αwith␈αit.␈α For␈αexample␈α ␈↓¬((X␈α.␈α5)␈α(Y␈α.␈α9.3)␈α(Z␈α.␈α2.1))␈↓.␈α To␈αlook␈αup␈αthe␈αvalue␈αof␈αa␈αvariable␈αin
␈↓ ↓H␈↓an a-list we use the function ␈↓↓assoc.␈↓ Thus
␈↓ ↓H␈↓␈↓ ∧J␈↓↓assoc[␈↓¬Y␈↓↓, ␈↓¬((X . 5) (Y . 9.3) (Z . 2.1))␈↓↓] = ␈↓¬9.3␈↓↓␈↓
␈↓ ↓H␈↓where ␈↓↓assoc␈↓ is given by
␈↓ ↓H␈↓11.2) ␈↓ β␈↓↓assoc[x,a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓a ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓a ␈↓αeq␈↓↓ x ␈↓αthen␈↓↓ ␈↓αa|␈↓↓a ␈↓αelse␈↓↓ assoc[x,␈↓αd|␈↓↓a].␈↓
␈↓ ↓H␈↓Association␈αlists␈αare␈αgenerally␈αuseful␈αfor␈αassociating␈α"values"␈αwith␈α"symbols"␈αand␈αthey␈α
will␈αappear
␈↓ ↓H␈↓in␈α
many␈α∞later␈α
examples.␈α∞ We␈α
note␈α∞here␈α
two␈α∞features␈α
which␈α∞are␈α
due␈α∞to␈α
the␈α∞way␈α
␈↓↓assoc␈↓␈α∞is␈α
defined.
␈↓ ↓H␈↓First,␈αnote␈αthat␈α␈↓↓assoc␈↓␈αreturns␈αthe␈αpair␈αrather␈αthan␈αthe␈αvalue␈αwhen␈αthe␈αdesired␈αsymbol␈αis␈αfound.␈α If
␈↓ ↓H␈↓no␈αvalue␈αis␈αfound␈α␈↓¬NIL␈↓␈αis␈αreturned.␈α This␈αis␈αso␈α
that␈αthe␈αcase␈αof␈αno␈αvalue␈αfound␈αcan␈αbe␈αdetected␈α
and
␈↓ ↓H␈↓still␈α
allow␈α∞the␈α
result␈α∞returned␈α
in␈α∞that␈α
case␈α∞to␈α
be␈α
associated␈α∞with␈α
a␈α∞symbol␈α
as␈α∞a␈α
value.␈α∞ Thus␈α
the
␈↓ ↓H␈↓calling␈α
program␈α
must␈α
check␈α∞to␈α
see␈α
if␈α
a␈α
value␈α∞was␈α
returned␈α
(␈↓↓¬␈↓αn|␈↓↓assoc[x,a]␈↓)␈α
and␈α
then␈α∞extract␈α
that
␈↓ ↓H␈↓value␈α∞(␈↓↓␈↓αd|␈↓↓assoc[x,a]␈↓).␈α∂ Also,␈α∞␈↓↓assoc␈↓␈α∂returns␈α∞the␈α∞first␈α∂pair␈α∞it␈α∂finds,␈α∞thus␈α∞a␈α∂symbol␈α∞can␈α∂be␈α∞associated
␈↓ ↓H␈↓with␈α⊃several␈α⊃values␈α⊃in␈α⊃the␈α⊃a-list,␈α⊃but␈α⊃always␈α⊃the␈α⊃first␈α⊃occurence␈α⊃determines␈α⊃the␈α⊃result␈α⊃that␈α⊂is
␈↓ ↓H␈↓returned. Thus
␈↓ ↓H␈↓␈↓ βb␈↓↓assoc[␈↓¬X␈↓↓,␈↓¬((U . 1.41) (X . 5) (Y . 9.3) (X . 1.2) (Z . 2.1))␈↓↓] = ␈↓¬5␈↓↓␈↓.
␈↓ ↓H␈↓␈↓↓assoc␈↓ is built into most LISP systems.
␈↓ ↓H␈↓ The interpreter, ␈↓↓numval,␈↓ can now be defined.
␈↓ ↓H␈↓24␈↓ εβChapter I␈↓ H
␈↓ ↓H␈↓␈↓ αx␈↓↓numval[e,a]␈↓␈↓ ∧8␈↓↓← ␈↓αif␈↓↓ numberp e ␈↓αthen␈↓↓ e␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ ␈↓αd|␈↓↓assoc[e,a]␈↓
␈↓ ↓H␈↓11.3)␈↓ ∧8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e ␈↓αeq␈↓↓ ␈↓¬PLUS ␈↓↓␈↓αthen␈↓↓ evplus[␈↓αd|␈↓↓e,a]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e ␈↓αeq␈↓↓ ␈↓¬TIMES ␈↓↓␈↓αthen␈↓↓ evtimes[␈↓αd|␈↓↓e,a]␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓11.4) ␈↓ αJ␈↓↓ evplus[u,a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ 0 ␈↓αelse␈↓↓ numval[␈↓αa|␈↓↓u,a] + evplus[␈↓αd|␈↓↓u,a], ␈↓
␈↓ ↓H␈↓11.5) ␈↓ αe␈↓↓evtimes[u,a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ numval[␈↓αa|␈↓↓u,a] ␈↓∧.␈↓↓ evtimes[␈↓αd|␈↓↓u,a], ␈↓
␈↓ ↓H␈↓12. ␈↓αThe function ␈↓↓eval.␈↓α ␈↓
␈↓ ↓H␈↓ ␈↓↓eval␈↓␈αplays␈αboth␈αa␈αtheoretical␈αand␈αa␈αpractical␈αrole␈αin␈αLISP.␈α Historically,␈αthe␈αlist␈αnotation␈αfor
␈↓ ↓H␈↓LISP␈αfunctions␈αand␈α␈↓↓eval␈↓␈α
were␈αfirst␈αdevised␈αin␈α
order␈αto␈αshow␈αhow␈αeasy␈α
it␈αis␈αto␈αdefine␈α
a␈αuniversal
␈↓ ↓H␈↓function␈α
in␈αLISP␈α
-␈αthe␈α
idea␈α
was␈αto␈α
advocate␈αLISP␈α
as␈α
an␈αalternative␈α
to␈αTuring␈α
machines␈αfor␈α
doing
␈↓ ↓H␈↓the␈α∂elementary␈α⊂theory␈α∂of␈α∂computability.␈α⊂ This␈α∂role␈α∂will␈α⊂be␈α∂discussed␈α∂in␈α⊂a␈α∂later␈α∂chapter.␈α⊂ S.␈α∂R.
␈↓ ↓H␈↓Russell␈α∞noted␈α∞that␈α∞␈↓↓eval␈↓␈α∞could␈α∞serve␈α∞as␈α∞an␈α∞interpreter␈α∞for␈α∞LISP␈α∞and␈α∞promptly␈α∞programmed␈α∞it␈α∞in
␈↓ ↓H␈↓machine␈α
language␈α
with␈α
minor␈α
modifications␈α
to␈α
make␈α
it␈α
more␈α
practical.␈α
An␈α
interpreter␈α
based␈α
on
␈↓ ↓H␈↓␈↓↓eval␈↓␈αhas␈αremained␈αa␈αfeature␈αof␈αmost␈αLISP␈αsystems.␈α Thus␈αwhen␈αyou␈αtalking␈αto␈αLISP␈αthe␈αsystem␈αis
␈↓ ↓H␈↓in␈αa␈αloop␈αthat␈α␈↓↓read␈↓s␈αwhat␈αyou␈αtype,␈α␈↓↓eval␈↓s␈αit␈αand␈α␈↓↓print␈↓s␈αthe␈αresult.␈α [Of␈αcourse␈αa␈αreal␈αLISP␈αsystem
␈↓ ↓H␈↓does many other things too, such a storage management, error handling, etc.]
␈↓ ↓H␈↓ ␈↓↓eval␈↓␈αfor␈αLISP␈αexpressions␈αis␈αanalogous␈αto␈αthe␈αinterpreter␈α␈↓↓numval␈↓␈αfor␈αarithmetic␈αexpressions
␈↓ ↓H␈↓given␈αin␈α(11.3).␈α
The␈αfirst␈αargument␈α
to␈α␈↓↓eval␈↓␈αis␈α
a␈αLISP␈αexpression␈α
in␈αinternal␈αnotation.␈α The␈α
second
␈↓ ↓H␈↓argument␈α∞is␈α∂an␈α∞association␈α∞list␈α∂that␈α∞tells␈α∞␈↓↓eval␈↓␈α∂what␈α∞value␈α∞each␈α∂variable␈α∞has,␈α∞and␈α∂what␈α∞function
␈↓ ↓H␈↓definition␈αis␈αto␈αbe␈αassociated␈αwith␈αeach␈αfunction␈αname.␈α Thus␈αthe␈αassociation␈αlist␈αis␈αa␈αlist␈αof␈αpairs
␈↓ ↓H␈↓where␈αeach␈α
pair␈αconsists␈α
either␈αof␈α
a␈αvariable␈α
and␈αthe␈α
S-expression␈αcorresponding␈α
to␈αits␈α
value,␈αor␈α
a
␈↓ ↓H␈↓function␈α
name␈α∞and␈α
the␈α∞S-expression␈α
representing␈α∞the␈α
function␈α∞expression␈α
defining␈α∞the␈α
function.
␈↓ ↓H␈↓(Here␈α∂a␈α∂function␈α∂expression␈α∂is␈α∞either␈α∂a␈α∂function␈α∂name,␈α∂or␈α∞a␈α∂lambda␈α∂expression.)␈α∂The␈α∂result␈α∞of
␈↓ ↓H␈↓applying␈α␈↓↓eval␈↓␈αis␈αthe␈αvalue␈αof␈αthe␈α
term␈αrepresented␈αby␈αthe␈αS-expression␈αin␈αan␈α
environment␈αwhere
␈↓ ↓H␈↓the␈α∞free␈α∂variables␈α∞are␈α∞assigned␈α∂the␈α∞values␈α∞given␈α∂by␈α∞the␈α∞association␈α∂list␈α∞and␈α∞where␈α∂the␈α∞function
␈↓ ↓H␈↓names␈α∪occuring␈α∩free␈α∪(i.e.␈α∩not␈α∪bound␈α∩in␈α∪a␈α∩label␈α∪expression)␈α∩denote␈α∪functions␈α∩defined␈α∪by␈α∩the
␈↓ ↓H␈↓associated expressions in the association list.
␈↓ ↓H␈↓ Since␈αany␈αcomputation␈αcan␈αbe␈αdescribed␈αas␈αevaluating␈αan␈αexpression␈αwithout␈αfree␈αvariables
␈↓ ↓H␈↓or␈α∀function␈α∀names,␈α∀the␈α∀second␈α∀argument␈α∀theoretically␈α∀plays␈α∀a␈α∀role␈α∀mainly␈α∀in␈α∃the␈α∀recursive
␈↓ ↓H␈↓definition of ␈↓↓eval.␈↓ Thus we usually start our computations with the second argument ␈↓¬NIL␈↓.
␈↓ ↓H␈↓ To␈αillustrate␈α
this,␈αsuppose␈α
we␈αwant␈αto␈α
apply␈αthe␈α
function␈α␈↓↓alt␈↓␈αto␈α
the␈αlist␈α
␈↓¬(A␈αB␈αC␈α
D␈αE)␈↓,␈α
i.e.␈αwe
␈↓ ↓H␈↓wish to evaluate ␈↓↓alt[␈↓¬(A B C D E)␈↓↓]␈↓. This can be obtained by computing
␈↓ ↓H␈↓ ␈↓↓eval[␈↓␈↓¬((LABEL ALT␈↓
␈↓ ↓H␈↓ ␈↓¬(LAMBDA (X) (COND ((OR (NULL X) (NULL (CDR X))) X)␈↓
␈↓ ↓H␈↓ ␈↓¬(T (CONS (CAR X) (ALT (CDDR X)))))))␈↓
␈↓ ↓H␈↓␈↓ εβChapter I␈↓ *25
␈↓ ↓H␈↓ ␈↓¬(QUOTE (A B C D E)))␈↓
␈↓ ↓H␈↓ ,␈↓↓␈↓¬NIL␈↓↓]␈↓,
␈↓ ↓H␈↓which gives the expected result ␈↓¬(A C E)␈↓.
␈↓ ↓H␈↓ This␈α∂manner␈α∞of␈α∂evaluating␈α∞requires␈α∂that␈α∞auxiliary␈α∂functions␈α∞must␈α∂be␈α∞defined␈α∂within␈α∞any
␈↓ ↓H␈↓expression␈αwhere␈αthey␈αare␈αused␈α(by␈αusing␈αthe␈αlabel␈αconstruct)␈αand␈αworse␈αyet,␈αthey␈αmust␈αbe␈αdefined
␈↓ ↓H␈↓separately␈α↔for␈α⊗separate␈α↔occurrences.␈α⊗ This␈α↔can␈α⊗become␈α↔very␈α⊗cumbersome␈α↔and␈α↔also␈α⊗makes
␈↓ ↓H␈↓expressions␈α∞fairly␈α∞difficult␈α
to␈α∞understand.␈α∞ Another␈α
approach␈α∞is␈α∞to␈α
use␈α∞an␈α∞association␈α∞list␈α
which
␈↓ ↓H␈↓associates␈α⊗each␈α∃function␈α⊗name␈α⊗appearing␈α∃in␈α⊗the␈α⊗expression␈α∃with␈α⊗the␈α⊗appropriate␈α∃defining
␈↓ ↓H␈↓expression. Thus we could evaluate ␈↓↓alt[␈↓¬(A B C D E)␈↓↓]␈↓ by computing
␈↓ ↓H␈↓↓ eval[␈↓¬(ALT (QUOTE (A B C D E)))␈↓↓,
␈↓ ↓H␈↓↓ ␈↓¬((ALT LAMBDA (X) (COND ((OR (NULL X) (NULL (CDR X))) X)␈↓↓
␈↓ ↓H␈↓↓ ␈↓¬(T (CONS (CAR X) (ALT (CDDR X)))))))␈↓↓].
␈↓ ↓H␈↓ A simplified version of the usual LISP ␈↓↓eval␈↓ is the following:
␈↓ ↓H␈↓␈↓ α8␈↓↓eval[e, a] ←␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ [␈↓αif␈↓↓ numberp e ∨ e = ␈↓¬NIL␈↓↓ ∨ e = ␈↓¬T␈↓↓ ␈↓αthen␈↓↓ e ␈↓αelse␈↓↓ ␈↓αd|␈↓↓assoc[e, a]]␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓e ␈↓αthen␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓[␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬QUOTE ␈↓↓␈↓αthen␈↓↓ ␈↓αad|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬COND ␈↓↓␈↓αthen␈↓↓ evcond[␈↓αd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬LIST ␈↓↓␈↓αthen␈↓↓ evlist[␈↓αd|␈↓↓e,a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CAR ␈↓↓␈↓αthen␈↓↓ ␈↓αa|␈↓↓eval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓12.1)␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CDR ␈↓↓␈↓αthen␈↓↓ ␈↓αd|␈↓↓eval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CONS ␈↓↓␈↓αthen␈↓↓ eval[␈↓αad|␈↓↓e, a] . eval[␈↓αadd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬ATOM ␈↓↓␈↓αthen␈↓↓ ␈↓αat|␈↓↓eval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬EQ ␈↓↓␈↓αthen␈↓↓ eval[␈↓αad|␈↓↓e, a] = eval[␈↓αadd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ eval[␈↓αd|␈↓↓assoc[␈↓αa|␈↓↓e, a] . ␈↓αd|␈↓↓e, a]]␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓e = ␈↓¬LAMBDA ␈↓↓␈↓αthen␈↓↓ eval[␈↓αadda|␈↓↓e, prup[␈↓αada|␈↓↓e, evlist[␈↓αd|␈↓↓e,a] * a]␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓e = ␈↓¬LABEL ␈↓↓␈↓αthen␈↓↓ eval[␈↓αadda|␈↓↓e . ␈↓αd|␈↓↓e, [␈↓αada|␈↓↓e . ␈↓αadda|␈↓↓e] . a]␈↓,
␈↓ ↓H␈↓where the auxiliary functions ␈↓↓evcond␈↓ and ␈↓↓evlist␈↓ are defined by
␈↓ ↓H␈↓12.2) ␈↓ α⎇␈↓↓evcond[u, a] ← ␈↓αif␈↓↓ eval[␈↓αaa|␈↓↓u, a] ␈↓αthen␈↓↓ eval[␈↓αada|␈↓↓u, a] ␈↓αelse␈↓↓ evcond[␈↓αd|␈↓↓u, a]␈↓,
␈↓ ↓H␈↓12.3) ␈↓ βA␈↓↓evlist[u, a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ eval[␈↓αa|␈↓↓u,a] . evlist[␈↓αd|␈↓↓u, a]␈↓,
␈↓ ↓H␈↓and␈αthe␈αauxiliary␈αfunction␈α␈↓↓prup,␈↓␈αused␈αfor␈αpairing␈αup␈αthe␈αelements␈αof␈αtwo␈αlists␈αof␈αequal␈αlength,␈αis
␈↓ ↓H␈↓defined by
␈↓ ↓H␈↓12.4) ␈↓ βF␈↓↓prup[u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [␈↓αa|␈↓↓u . ␈↓αa|␈↓↓v] . prup[␈↓αd|␈↓↓u,␈↓αd|␈↓↓v]␈↓.
␈↓ ↓H␈↓␈↓↓assoc␈↓ was given in (11.2).
␈↓ ↓H␈↓ This␈α⊃simple␈α⊃␈↓↓eval␈↓␈α⊃expects␈α∩that␈α⊃an␈α⊃expression␈α⊃is␈α⊃either␈α∩a␈α⊃constant␈α⊃(␈↓↓number,␈↓␈α⊃␈↓¬T␈↓,␈α⊃␈↓¬NIL␈↓,␈α∩or␈α⊃a
␈↓ ↓H␈↓␈↓¬QUOTE␈↓d␈αS-expression),␈αa␈αvariable␈αwhose␈αvalue␈αcan␈αbe␈αfound␈αon␈αthe␈αassociation␈αlist,␈α
a␈αconditional
␈↓ ↓H␈↓expression,␈α
a␈αlist␈α
making␈αexpression,␈α
or␈αan␈α
application␈αof␈α
a␈αfunction,␈α
lambda␈αexpression␈α
or␈αlabel
␈↓ ↓H␈↓26␈↓ εβChapter I␈↓ H
␈↓ ↓H␈↓expression␈α∩to␈α∩a␈α∩list␈α∩of␈α∩arguments.␈α∩ Thus␈α∪␈↓↓eval␈↓␈α∩checks␈α∩to␈α∩see␈α∩which␈α∩of␈α∩the␈α∩above␈α∪classes␈α∩the
␈↓ ↓H␈↓expression␈α
to␈α
be␈α
evaluated␈α
falls␈α
into␈α
and␈α
proceeds␈α
accordingly.␈α
If␈α
the␈α
expression,␈α
␈↓↓e,␈↓␈α
is␈α
atomic␈α
then
␈↓ ↓H␈↓it␈α∩is␈α∩either␈α⊃a␈α∩non␈α∩␈↓¬QUOTE␈↓d␈α⊃constant␈α∩or␈α∩a␈α∩variable.␈α⊃ If␈α∩the␈α∩former␈α⊃then␈α∩␈↓↓eval␈↓␈α∩just␈α∩returns␈α⊃the
␈↓ ↓H␈↓expression, if the latter it looks up the value on the association list, ␈↓↓a.␈↓
␈↓ ↓H␈↓ If␈α␈↓↓e␈↓␈αis␈αnon-atomic␈αbut␈α␈↓αa|␈↓␈↓↓e␈↓␈αis␈αatomic␈αthen␈α␈↓↓e␈↓␈αis␈αeither␈αa␈α␈↓¬QUOTE␈↓d␈αconstant,␈αa␈αconditional,␈α
a␈αlist
␈↓ ↓H␈↓maker,␈αor␈αan␈αapplication␈αof␈αa␈αfunction␈αto␈αa␈αlist␈αof␈αarguments.␈α In␈αthe␈αconstant␈αcase␈αthe␈αexpression
␈↓ ↓H␈↓being␈α∂quoted␈α∞(␈↓αad|␈↓␈↓↓e)␈↓␈α∂is␈α∞returned.␈α∂ If␈α∞␈↓↓e␈↓␈α∂is␈α∂a␈α∞conditional␈α∂then␈α∞the␈α∂list␈α∞of␈α∂pairs␈α∞is␈α∂processed␈α∂by␈α∞the
␈↓ ↓H␈↓auxiliary␈α
evaluator␈α
␈↓↓evcond,␈↓␈α
which␈α
␈↓↓eval␈↓s␈α
the␈α
"if"␈α
parts␈α
(␈↓↓␈↓αaa|␈↓↓u␈↓)␈α
in␈α
order␈α
until␈α
a␈α
true␈α
one␈α
is␈α
found␈α
then
␈↓ ↓H␈↓returns␈α⊂the␈α∂result␈α⊂of␈α∂␈↓↓eval␈↓ing␈α⊂the␈α∂corresponding␈α⊂"then"␈α∂part␈α⊂(␈↓↓␈↓αada|␈↓↓u␈↓).␈α∂ In␈α⊂the␈α∂list␈α⊂case␈α∂the␈α⊂list␈α∂of
␈↓ ↓H␈↓expressions␈α
to␈α
be␈α
evaluated␈α
is␈α
given␈α
to␈α
␈↓↓evlist␈↓␈α
which␈α
returns␈α
a␈α
list␈α
of␈α
the␈α
values.␈α
␈↓¬COND␈α∞␈↓and␈α
␈↓¬LIST
␈↓ ↓H␈↓¬␈↓considered␈αas␈αpseudo-functions␈αbehave␈αdifferently␈αthan␈αordinary␈αfunctions␈αin␈αthe␈αfollowing␈αways.
␈↓ ↓H␈↓First␈α⊃both␈α⊂can␈α⊃take␈α⊃an␈α⊂arbitrary␈α⊃number␈α⊃of␈α⊂arguments␈α⊃while␈α⊃functions␈α⊂defined␈α⊃by␈α⊃a␈α⊂lambda
␈↓ ↓H␈↓expression␈α∂have␈α∂a␈α∂fixed␈α∂number␈α∂of␈α∂arguments␈α∞determined␈α∂by␈α∂the␈α∂variable␈α∂list␈α∂occuring␈α∂in␈α∞the
␈↓ ↓H␈↓lambda␈α∂expression.␈α∞ Second,␈α∂the␈α∞usual␈α∂manner␈α∂of␈α∞evaluation␈α∂an␈α∞application␈α∂term␈α∞is␈α∂LISP␈α∂is␈α∞to
␈↓ ↓H␈↓evaluate␈αall␈αof␈αthe␈αarguments␈αthen␈αapply␈αthe␈αfunction.␈α This␈αwill␈αnot␈αwork␈αfor␈α␈↓¬COND␈α␈↓as␈αthe␈αmain
␈↓ ↓H␈↓reason␈α⊂for␈α⊂a␈α⊂conditional␈α⊂is␈α⊃to␈α⊂be␈α⊂able␈α⊂to␈α⊂select␈α⊂a␈α⊃term␈α⊂to␈α⊂evaluate␈α⊂depending␈α⊂on␈α⊂some␈α⊃set␈α⊂of
␈↓ ↓H␈↓conditions␈α∞and␈α∞not␈α∞to␈α∂evaluate␈α∞other␈α∞terms␈α∞under␈α∂those␈α∞conditions.␈α∞ In␈α∞the␈α∂function␈α∞application
␈↓ ↓H␈↓case␈α
there␈α
are␈αtwo␈α
possibilities.␈α
If␈αthe␈α
function␈α
to␈αbe␈α
applied␈α
is␈αone␈α
of␈α
the␈α
elementary␈αfunctions,
␈↓ ↓H␈↓the␈α∞indicated␈α∞operation␈α∞is␈α∞performed␈α∞on␈α∞the␈α∞result␈α∞of␈α∞evaluating␈α∞the␈α∞arguments.␈α∞ Otherwise␈α∞the
␈↓ ↓H␈↓function␈αmust␈αbe␈αdefined␈αin␈α␈↓↓a,␈↓␈αso␈α␈↓↓eval␈↓␈αlooks␈αup␈αthe␈αdefinition,␈αreplaces␈αthe␈αfunction␈αname␈αby␈αthe
␈↓ ↓H␈↓function definition in the expression and restarts the evaluation.
␈↓ ↓H␈↓ If␈α
neither␈α␈↓↓e␈↓␈α
nor␈α␈↓αa|␈↓␈↓↓e␈↓␈α
are␈αatomic␈α
then␈αit␈α
must␈α
be␈αa␈α
lambda␈αor␈α
label␈αapplication.␈α
In␈αthe␈α
lambda
␈↓ ↓H␈↓case␈αthe␈αargument␈α
list␈αis␈αgiven␈αto␈α
␈↓↓evlist␈↓␈αto␈αbe␈αevaluated,␈α
the␈αvalues␈αare␈αthen␈α
paired␈αwith␈αthe␈αlist␈α
of
␈↓ ↓H␈↓variables␈αto␈αbe␈αbound␈αby␈αthe␈αlambda␈α(␈↓αada|␈↓␈↓↓e)␈↓␈αusing␈α␈↓↓prup␈↓␈αand␈αput␈αon␈αthe␈αfront␈αof␈α␈↓↓a.␈↓␈αThe␈αbody␈αof
␈↓ ↓H␈↓the␈α∂lambda␈α∂(␈↓αadda|␈↓␈↓↓e)␈↓␈α∂is␈α∂then␈α⊂evaluated␈α∂using␈α∂this␈α∂new␈α∂association␈α⊂list.␈α∂ In␈α∂the␈α∂label␈α∂case␈α⊂a␈α∂new
␈↓ ↓H␈↓association␈α∩list␈α∩is␈α∩formed␈α∩by␈α∩pairing␈α⊃the␈α∩function␈α∩name␈α∩(␈↓αada|␈↓␈↓↓e)␈↓␈α∩with␈α∩the␈α∩defining␈α⊃expression
␈↓ ↓H␈↓(␈↓αadda|␈↓␈↓↓e)␈↓␈αand␈α
adding␈αthe␈α
result␈αto␈α
the␈αfront␈α
of␈α␈↓↓a.␈↓␈α
Then␈αthe␈α
label␈αexpression␈α
is␈αreplaced␈α
in␈α␈↓↓e␈↓␈αby␈α
the
␈↓ ↓H␈↓defining expression and this is evaluated using the new association list.
␈↓ ↓H␈↓ If␈α∞␈↓↓e␈↓␈α
is␈α∞not␈α∞an␈α
expression␈α∞of␈α
the␈α∞sort␈α∞expected␈α
by␈α∞␈↓↓eval,␈↓␈α
then␈α∞the␈α∞result␈α
is␈α∞not␈α∞defined.␈α
It
␈↓ ↓H␈↓would␈α
not␈α
be␈α
difficult␈α
to␈α
add␈α
additional␈α∞clauses␈α
to␈α
␈↓↓eval␈↓␈α
so␈α
that␈α
it␈α
would␈α
return␈α∞reasonable␈α
error
␈↓ ↓H␈↓messages␈αrather␈α
than␈αjust␈α
being␈αundefined␈α(or␈α
dying␈αin␈α
some␈αstrange␈αway␈α
as␈αwould␈α
be␈αlikely␈αin␈α
an
␈↓ ↓H␈↓actual computer).
␈↓ ↓H␈↓ We␈α
note␈α∞that␈α
␈↓↓eval␈↓␈α
can␈α∞evaluate␈α
itself␈α∞if␈α
it␈α
is␈α∞given␈α
an␈α
association␈α∞list␈α
containing␈α∞the␈α
pairs
␈↓ ↓H␈↓␈↓¬(EVAL . λeval),␈α≥␈↓␈↓¬(EVCOND . λevcond),␈α≡␈↓␈↓¬(EVLIST . λevlist),␈α≥␈↓␈α≥␈↓¬(ASSOC . λassoc),␈α≡␈↓␈↓¬(PRUP . λprup),␈α≥␈↓and
␈↓ ↓H␈↓␈↓¬(APPEND . λappend)␈α∀␈↓␈α∪where␈α∀␈↓¬λ<function name>␈α∀␈↓stands␈α∪for␈α∀the␈α∀internal␈α∪form␈α∀of␈α∀the␈α∪expression
␈↓ ↓H␈↓defining the named function. Thus
␈↓ ↓H␈↓␈↓ ∧T␈↓↓eval[␈↓¬(EVAL '(CAR '(A.B)) NIL)␈↓↓,alist] = ␈↓¬A␈↓↓␈↓
␈↓ ↓H␈↓where␈α
␈↓↓alist␈↓␈αis␈α
some␈αassociation␈α
list␈αcontaining␈α
the␈α
above␈αmentioned␈α
pairs␈α(and␈α
no␈αother␈α
definitions
␈↓ ↓H␈↓of those functions).
␈↓ ↓H␈↓ When␈αyou␈αtalk␈αto␈αLISP␈αyou␈αdo␈αnot␈αexplicitly␈αtell␈αthe␈αinterpreter␈αwhat␈αassociation␈αlist␈αto␈αuse
␈↓ ↓H␈↓generally.␈α∞ This␈α∞is␈α
because␈α∞the␈α∞LISP␈α
interpreter␈α∞typically␈α∞looks␈α
up␈α∞variable␈α∞values␈α∞and␈α
function
␈↓ ↓H␈↓definitions␈α∞on␈α∂the␈α∞corresponding␈α∞property␈α∂lists.␈α∞ Thus,␈α∂instead␈α∞of␈α∞making␈α∂up␈α∞an␈α∂association␈α∞list
␈↓ ↓H␈↓with␈α
the␈α
appropriate␈α
variables␈α
and␈α
functions␈α
defined,␈α
the␈α
property␈α
lists␈α
must␈α
first␈α
be␈α
primed␈α
by
␈↓ ↓H␈↓␈↓ εβChapter I␈↓ *27
␈↓ ↓H␈↓doing␈α
␈↓¬SETQ␈↓s␈αfor␈α
giving␈αvariables␈α
initial␈α
values␈αand␈α
␈↓¬DEFUN␈↓s␈α(or␈α
␈↓¬DEFPROP␈↓s)␈αfor␈α
any␈α
functions␈αthat
␈↓ ↓H␈↓need to be defined. These features will be discussed in more detail in Chapter IV.
␈↓ ↓H␈↓28␈↓ εH␈↓ H
␈↓ ↓H␈↓α␈↓ εChapter II
␈↓ ↓H␈↓α␈↓ β&HOW TO WRITE RECURSIVE FUNCTION DEFINITIONS
␈↓ ↓H␈↓1. ␈↓αStatic and dynamic ways of programming.␈↓
␈↓ ↓H␈↓ In␈α∃order␈α∀to␈α∃write␈α∀recursive␈α∃function␈α∀definitions,␈α∃one␈α∀must␈α∃think␈α∃about␈α∀programming
␈↓ ↓H␈↓differently␈α
than␈α
is␈α
customary␈α
when␈α
writing␈α
programs␈α
in␈α
languages␈α
like␈α
FORTRAN␈α
or␈αALGOL␈α
or
␈↓ ↓H␈↓in␈α⊃machine␈α∩language.␈α⊃ In␈α∩these␈α⊃languages,␈α⊃one␈α∩has␈α⊃in␈α∩mind␈α⊃the␈α⊃state␈α∩of␈α⊃the␈α∩computation␈α⊃as
␈↓ ↓H␈↓represented␈α∞by␈α∂the␈α∞values␈α∂of␈α∞certain␈α∞variables␈α∂or␈α∞locations␈α∂in␈α∞the␈α∞memory␈α∂of␈α∞the␈α∂machine,␈α∞and
␈↓ ↓H␈↓then␈α⊃one␈α⊃writes␈α⊃statements␈α⊃or␈α⊃machine␈α⊃instructions␈α∩in␈α⊃order␈α⊃to␈α⊃make␈α⊃the␈α⊃state␈α⊃change␈α∩in␈α⊃an
␈↓ ↓H␈↓appropriate way.
␈↓ ↓H␈↓ When␈α∞writing␈α
LISP␈α∞recursive␈α∞functions␈α
one␈α∞thinks␈α∞differently.␈α
Namely,␈α∞one␈α∞thinks␈α
about
␈↓ ↓H␈↓the␈α∂value␈α∂of␈α∂the␈α∂function,␈α∂asks␈α∂for␈α∂what␈α∂values␈α∂of␈α∂the␈α∂arguments␈α∂the␈α∂value␈α∂of␈α∂the␈α∂function␈α∞is
␈↓ ↓H␈↓immediate,␈α∞and,␈α∞given␈α∞arbitrary␈α∂values␈α∞of␈α∞the␈α∞arguments,␈α∂for␈α∞what␈α∞simpler␈α∞arguments␈α∂must␈α∞the
␈↓ ↓H␈↓function␈α
be␈α
known␈α
in␈α
order␈α
to␈α
give␈α
the␈α
value␈α
of␈α
the␈α
function␈α
for␈α
the␈α
given␈α
arguments.␈α
Let␈α
us␈α
take
␈↓ ↓H␈↓a␈α
numerical␈αexample;␈α
namely,␈αsuppose␈α
we␈αwant␈α
to␈αcompute␈α
the␈αfunction␈α
␈↓↓n!.␈↓␈α For␈α
what␈αargument␈α
is
␈↓ ↓H␈↓the␈αvalue␈αof␈αthe␈αfunction␈αimmediate.␈α Clearly,␈αfor␈α␈↓↓n␈↓␈α=␈α0␈αor␈α␈↓↓n␈↓␈α=␈α1,␈αthe␈αvalue␈αis␈αimmediately␈αseen␈αto
␈↓ ↓H␈↓be␈α1.␈α Moreover,␈αwe␈αcan␈αget␈αthe␈αvalue␈αfor␈αan␈αarbitrary␈α␈↓↓n␈↓␈αif␈αwe␈αknow␈αthe␈αvalue␈αfor␈α␈↓↓n-1␈↓.␈α Also,␈αwe
␈↓ ↓H␈↓see␈α
that␈αknowing␈α
the␈α
value␈αfor␈α
␈↓↓n␈↓␈α
=␈α1␈α
is␈αredundant,␈α
since␈α
it␈αcan␈α
be␈α
obtained␈αfrom␈α
the␈α
␈↓↓n␈↓␈α=␈α
0␈αcase␈α
by
␈↓ ↓H␈↓the␈αsame␈αrule␈αas␈αgets␈αit␈αfor␈αa␈αgeneral␈α␈↓↓n␈↓␈αfrom␈αthe␈αvalue␈αfor␈α␈↓↓n-1␈↓.␈α All␈αthis␈αtalk␈αleads␈αto␈αthe␈αsimple
␈↓ ↓H␈↓recursive equation.
␈↓ ↓H␈↓␈↓ ∧\␈↓↓n! ← ␈↓αif␈↓↓ n = 0 ␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ n(n-1)! ␈↓.
␈↓ ↓H␈↓ We␈αmay␈αregard␈αthis␈αas␈αa␈αstatic␈αway␈αof␈αlooking␈αat␈αprogramming.␈α We␈αask␈αwhat␈αsimpler␈α
cases
␈↓ ↓H␈↓the␈αgeneral␈αcase␈αof␈αour␈αfunction␈αdepends␈αon␈αrather␈αthan␈αhow␈αwe␈αbuild␈αup␈αthe␈αdesired␈αstate␈αof␈αthe
␈↓ ↓H␈↓computation.␈α One␈αoften␈αis␈αled␈αto␈αbelieve␈αthat␈αstatic␈α=␈αbad␈αand␈α dynamic␈α=␈αgood,␈αbut␈αin␈αthis␈αcase,
␈↓ ↓H␈↓the␈α
static␈α
way␈αis␈α
often␈α
better␈αthan␈α
the␈α
dynamic␈αway.␈α
Perhaps␈α
this␈αdistinction␈α
is␈α
equivalent␈αto␈α
what
␈↓ ↓H␈↓some␈α∪people␈α∪call␈α∀the␈α∪distinction␈α∪between␈α∪␈↓↓top-down␈↓␈α∀and␈α∪␈↓↓bottom-up␈↓␈α∪programming␈α∀with␈α∪␈↓↓static␈↓
␈↓ ↓H␈↓corresponding␈α∞to␈α∞␈↓↓top-down.␈↓␈α∞ LISP␈α∞offers␈α∞both,␈α∞but␈α∞the␈α∞static␈α∞style␈α∞is␈α∞better␈α∞developed␈α∞in␈α∞LISP,
␈↓ ↓H␈↓and we will emphasize it.
␈↓ ↓H␈↓ Compare␈α∞the␈α∞above␈α∞recursive␈α∞definition␈α
with␈α∞the␈α∞following␈α∞obvious␈α∞ALGOL␈α∞60␈α
program
␈↓ ↓H␈↓for computing ␈↓↓n!:␈↓
␈↓ ↓H␈↓↓␈↓ ∧H␈↓αinteger procedure␈↓↓ factorial(n); ␈↓αinteger␈↓↓ s;
␈↓ ↓H␈↓↓␈↓ βx␈↓αbegin␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧Hs := 1;
␈↓ ↓H␈↓↓␈↓ βxloop:␈↓ ∧H␈↓αif␈↓↓ n = 0 ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ done;
␈↓ ↓H␈↓↓␈↓ ∧Hs := n*s;
␈↓ ↓H␈↓↓␈↓ ∧Hn := n-1;
␈↓ ↓H␈↓↓␈↓ ∧H␈↓αgo to␈↓↓ loop;
␈↓ ↓H␈↓↓␈↓ βxdone:␈↓ ∧Hfactorial := s;
␈↓ ↓H␈↓↓␈↓ βx␈↓αend␈↓↓;
␈↓ ↓H␈↓␈↓ ¬}Chapter II␈↓ *29
␈↓ ↓H␈↓ The␈α∂LISP␈α∂program␈α∂is␈α∂shorter␈α⊂and␈α∂clearer␈α∂in␈α∂this␈α∂(particularly␈α∂favorable)␈α⊂case.␈α∂ Actually,
␈↓ ↓H␈↓when␈αwe␈α
discuss␈αthe␈αmechanism␈α
of␈αrecursion,␈αit␈α
will␈αturn␈α
out␈αthat␈αthis␈α
LISP␈αprogram␈αis␈α
inefficient
␈↓ ↓H␈↓because␈α
it␈α
uses␈αthe␈α
pushdown␈α
mechanism␈αunnecessarily␈α
and␈α
should␈αbe␈α
replaced␈α
by␈α
the␈αfollowing
␈↓ ↓H␈↓somewhat longer program that corresponds to the above ALGOL program rather precisely:
␈↓ ↓H␈↓␈↓ ∧.␈↓↓ n! ← fact(n, 1), ␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ ∧↔␈↓↓fact(n, s) ← ␈↓αif␈↓↓ n = 0 ␈↓αthen␈↓↓ s ␈↓αelse␈↓↓ fact(n-1, n␈↓π*␈↓↓s)␈↓.
␈↓ ↓H␈↓In fact, compilers should produce the same object code from the two programs.
␈↓ ↓H␈↓2. ␈↓αSimple list recursion.␈↓
␈↓ ↓H␈↓ About␈α
the␈α
simplest␈α
form␈α
of␈α
recursion␈α
in␈α
LISP␈αoccurs␈α
when␈α
one␈α
of␈α
the␈α
arguments␈α
is␈α
a␈αlist,
␈↓ ↓H␈↓the␈α
result␈α
is␈αimmediate␈α
when␈α
the␈αargument␈α
is␈α
null,␈α
and␈αotherwise␈α
we␈α
need␈αonly␈α
know␈α
the␈αresult␈α
for
␈↓ ↓H␈↓the␈α∂d-part␈α∂of␈α∂that␈α∂argument.␈α∂ Several␈α∂of␈α∂the␈α∂functions␈α∂defined␈α∂in␈α∂Chapter␈α∂I␈α∂are␈α∂of␈α⊂this␈α∂form.
␈↓ ↓H␈↓Consider,␈α
for␈α
example,␈α
␈↓↓u*v␈↓,␈α
the␈α
result␈α
of␈α
␈↓↓append␈↓ing␈α
the␈α
list␈α
␈↓↓v␈↓␈α
to␈α
the␈α
list␈α
␈↓↓u.␈↓␈α
The␈α
result␈αis␈α
immediate
␈↓ ↓H␈↓for the case ␈↓αn|␈↓␈↓↓u␈↓ and otherwise depends on the result for ␈↓αd|␈↓␈↓↓u.␈↓ Thus, we have
␈↓ ↓H␈↓␈↓ ∧M␈↓↓u*v ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]␈↓.
␈↓ ↓H␈↓Notice␈α
that␈α∞if␈α
we␈α∞had␈α
tried␈α
to␈α∞recur␈α
on␈α∞␈↓↓v␈↓␈α
rather␈α∞than␈α
on␈α
␈↓↓u␈↓␈α∞we␈α
would␈α∞not␈α
have␈α∞been␈α
successful.
␈↓ ↓H␈↓The␈α∞result␈α
would␈α∞be␈α∞immediate␈α
for␈α∞␈↓αn|␈↓␈↓↓v,␈↓␈α∞but␈α
␈↓↓u*v␈↓␈α∞cannot␈α
be␈α∞constructed␈α∞in␈α
any␈α∞direct␈α∞way␈α
from
␈↓ ↓H␈↓␈↓↓u*␈↓αd|␈↓↓v␈↓␈αwithout␈αa␈αfunction␈αthat␈αputs␈αan␈αelement␈αonto␈αthe␈αend␈αof␈αa␈αlist.␈α (From␈αa␈αstrictly␈αlist␈αpoint␈αof
␈↓ ↓H␈↓view,␈αsuch␈α
a␈αfunction␈αwould␈α
be␈αas␈αelementary␈α
as␈α␈↓↓cons␈↓␈αwhich␈α
puts␈αan␈αelement␈α
onto␈αthe␈αfront␈α
of␈αa
␈↓ ↓H␈↓list,␈αbut,␈α
when␈αwe␈α
consider␈αthe␈αimplementation␈α
of␈αlists␈α
by␈αlist␈αstructures,␈α
we␈αsee␈α
that␈αthe␈αfunction␈α
is
␈↓ ↓H␈↓not␈αso␈αelementary.␈α This␈αhas␈αled␈αsome␈αpeople␈αto␈αconstruct␈αsystems␈αin␈αwhich␈αlists␈αare␈αbi-directional,
␈↓ ↓H␈↓but,␈αin␈αthe␈α
main,␈αthis␈αhas␈α
turned␈αout␈αto␈α
be␈αa␈αbad␈αidea).␈α
Anyway,␈αit␈αis␈α
usually␈αeasier␈αto␈α
recur␈αon
␈↓ ↓H␈↓one argument of a function than to recur on the other.
␈↓ ↓H␈↓Another␈α
example␈α
is␈αthe␈α
function␈α
␈↓↓member␈↓␈αthat␈α
tests␈α
for␈α
membership␈αin␈α
a␈α
list.␈α If␈α
the␈α
list␈α
is␈αempty
␈↓ ↓H␈↓then␈αthe␈αanswer␈αis␈α␈↓¬NIL␈↓␈α
otherwise␈αeither␈αthe␈αelement␈αwe␈αare␈α
searching␈αfor␈αis␈αthe␈αfirst␈αthing␈α
on␈αthe
␈↓ ↓H␈↓list, or it in the rest of the list. This reasoning gives rise to the recursive definition
␈↓ ↓H␈↓␈↓ βI␈↓↓member[x,u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ x = ␈↓αa|␈↓↓u ∨ member[x,␈↓αd|␈↓↓u]␈↓.
␈↓ ↓H␈↓The␈α∂reader␈α∂should␈α⊂convince␈α∂himself␈α∂that␈α⊂this␈α∂definition␈α∂is␈α∂equivalent␈α⊂the␈α∂that␈α∂given␈α⊂in␈α∂(I.8.6)
␈↓ ↓H␈↓using only logical connectives.
␈↓ ↓H␈↓ The␈αfunctions␈α␈↓↓last␈↓␈α(I.8.2)␈αwhich␈αcomputes␈αthe␈α
last␈αelement␈αof␈αa␈αlist␈αand␈α␈↓↓assoc␈↓␈α
(I.11.2)␈αwhich
␈↓ ↓H␈↓determines␈α∞what␈α∞expression␈α∂if␈α∞any␈α∞is␈α∞associated␈α∂with␈α∞an␈α∞atom␈α∞in␈α∂an␈α∞association␈α∞list␈α∂are␈α∞further
␈↓ ↓H␈↓examples of simple list recursion.
␈↓ ↓H␈↓ The␈αfunction␈α␈↓↓reverse␈↓␈α
is␈αanother␈αexample␈α
of␈αlist␈αrecursion␈α
The␈αstraight␈αforward␈αdefinition␈α
of
␈↓ ↓H␈↓␈↓↓reverse␈↓␈α(I.8.9)␈αinvolves␈α
␈↓↓append␈↓ing␈αthe␈α␈↓↓reverse␈↓␈α
of␈αthe␈αrest␈αof␈α
the␈αlist␈αto␈α
the␈αlist␈αcontaining␈α
only␈αthe
␈↓ ↓H␈↓30␈↓ ¬}Chapter II␈↓ H
␈↓ ↓H␈↓first␈αelement␈α
of␈αthe␈αlist.␈α
By␈αusing␈αan␈α
auxiliary␈αvariable␈α␈↓↓reverse␈↓␈α
can␈αbe␈αdefined␈α
directly␈αin␈αterms␈α
of
␈↓ ↓H␈↓itself and the basic ␈↓↓cons␈↓ operation (I.8.7).
␈↓ ↓H␈↓ The␈α
function␈α
␈↓↓alt␈↓␈α(I.8.1)␈α
which␈α
returns␈αa␈α
list␈α
of␈α
alternate␈αelements␈α
of␈α
a␈αlist␈α
is␈α
also␈α
based␈αon
␈↓ ↓H␈↓list␈αrecursion,␈α
however␈αthe␈αform␈α
of␈αthe␈α
recursion␈αis␈αdifferent.␈α
Here␈αthe␈αbase␈α
cases␈αare␈α
the␈αempty
␈↓ ↓H␈↓list␈αand␈αthe␈αlist␈αcontaining␈αone␈αelement.␈α In␈αthe␈αgeneral␈αcase␈αwe␈αuse␈αthe␈αresult␈αfor␈α␈↓αdd|␈↓␈↓↓u␈↓␈αto␈αcompute
␈↓ ↓H␈↓the result for ␈↓↓u.␈↓
␈↓ ↓H␈↓α␈↓ ε
Exercises
␈↓ ↓H␈↓ 1.␈αUsing␈αthe␈αfunction␈α␈↓↓member[x,␈αu]␈↓␈αdefined␈αin␈α
Chapter␈αI␈αwhich␈αmay␈αalso␈αbe␈αwritten␈α␈↓↓x␈α
ε␈αu␈↓,
␈↓ ↓H␈↓write␈αfunction␈αdefinitions␈αfor␈αthe␈αunion␈α␈↓↓u␈α∪␈αv␈↓␈αof␈αlists␈α␈↓↓u␈↓␈αand␈α␈↓↓v,␈↓␈αthe␈αintersection␈α␈↓↓u␈α∩␈αv␈↓,␈αand␈αthe␈αset
␈↓ ↓H␈↓difference ␈↓↓u-v␈↓. What is wanted should be clear from the examples:
␈↓ ↓H␈↓ ␈↓¬(A B C) ∪ (B C D) = (A B C D)␈↓,
␈↓ ↓H␈↓ ␈↓¬(A B C) ∩ (B C D) = (B C)␈↓,
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓¬(A B C) - (B C D) = (A)␈↓.
␈↓ ↓H␈↓Pay␈α⊂attention␈α⊂to␈α⊂getting␈α⊂correct␈α⊂the␈α⊂trivial␈α⊂cases␈α∂in␈α⊂which␈α⊂some␈α⊂of␈α⊂the␈α⊂arguments␈α⊂are␈α⊂␈↓¬NIL␈↓.␈α∂ In
␈↓ ↓H␈↓general, it is important to understand clearly the trivial cases of functions.
␈↓ ↓H␈↓ 2.␈α Suppose␈α ␈↓↓x␈↓␈α
takes␈αnumbers␈αas␈α
values␈αand␈α␈↓↓u␈↓␈α
takes␈αas␈αvalues␈α
lists␈αof␈αnumbers␈αin␈α
ascending
␈↓ ↓H␈↓order,␈α∞e.g.␈α∞ ␈↓¬(2␈α
4␈α∞7)␈↓.␈α∞ Write␈α∞a␈α
function␈α∞ ␈↓↓merge[x,␈α∞u]␈↓␈α
whose␈α∞value␈α∞is␈α∞obtained␈α
from␈α∞that␈α∞of␈α∞␈↓↓u␈↓␈α
by
␈↓ ↓H␈↓putting ␈↓↓x␈↓ in ␈↓↓u␈↓ in its proper place. Thus ␈↓↓merge[␈↓¬3, ␈↓↓␈↓¬(2 4)␈↓↓] = ␈↓¬(2 3 4)␈↓↓␈↓, and ␈↓↓merge[␈↓¬3, ␈↓↓␈↓¬(2 3)␈↓↓] = ␈↓¬(2 3 3)␈↓↓␈↓.
␈↓ ↓H␈↓ 3.␈α∂ Write␈α∂functions␈α∂giving␈α∂the␈α∂union,␈α∞intersection,␈α∂and␈α∂set␈α∂difference␈α∂of␈α∂ordered␈α∂lists;␈α∞the
␈↓ ↓H␈↓result is wanted as an ordered list.
␈↓ ↓H␈↓ Note␈α⊃that␈α⊃computing␈α⊃these␈α⊂functions␈α⊃of␈α⊃unordered␈α⊃lists␈α⊂takes␈α⊃a␈α⊃number␈α⊃of␈α⊂comparisons
␈↓ ↓H␈↓proportional␈αto␈αthe␈αsquare␈αof␈αthe␈αnumber␈αof␈αelements␈αof␈αa␈αtypical␈αlist,␈αwhile␈αfor␈αordered␈αlists,␈αthe
␈↓ ↓H␈↓number of comparisons is proportional to the number of elements.
␈↓ ↓H␈↓ 4.␈α∞ Using␈α
␈↓↓merge,␈↓␈α∞write␈α
a␈α∞function␈α
␈↓↓sort␈↓␈α∞that␈α
transforms␈α∞an␈α
unordered␈α∞list␈α
into␈α∞an␈α
ordered
␈↓ ↓H␈↓list.
␈↓ ↓H␈↓ 5.␈αWrite␈αa␈αfunction␈α␈↓↓goodsort␈↓␈αthat␈αsorts␈αa␈αlist␈αusing␈αa␈αnumber␈αof␈αcomparisons␈αproportional␈αto
␈↓ ↓H␈↓␈↓↓n log n␈↓, where ␈↓↓n␈↓ is the length of the list to be sorted.
␈↓ ↓H␈↓3. ␈↓αSimple S-expression recursion.␈↓
␈↓ ↓H␈↓ In␈α
another␈α
class␈αof␈α
problems,␈α
the␈α
value␈αof␈α
the␈α
function␈α
is␈αimmediate␈α
for␈α
atomic␈αsymbols,␈α
and
␈↓ ↓H␈↓for␈αnon␈αatoms␈αdepends␈αonly␈αon␈αthe␈αvalues␈αfor␈αthe␈αa-part␈αand␈αthe␈αd-part␈αof␈αthe␈αargument.␈α Thus
␈↓ ↓H␈↓␈↓↓subst␈↓ was defined by
␈↓ ↓H␈↓␈↓ ¬}Chapter II␈↓ *31
␈↓ ↓H␈↓␈↓ ↓q␈↓↓subst[x, y, z] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓z ␈↓αthen␈↓↓ [␈↓αif␈↓↓ z ␈↓αeq␈↓↓ y ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ z] ␈↓αelse␈↓↓ subst[x, y, ␈↓αa|␈↓↓z] . subst[x , y , ␈↓αd|␈↓↓z]␈↓.
␈↓ ↓H␈↓ Two␈αother␈αexamples␈αare␈α␈↓↓equal␈↓␈αwhich␈αgives␈αthe␈αequality␈αof␈αS-expressions␈αand␈α␈↓↓flatten␈↓␈αwhich
␈↓ ↓H␈↓spreads an S-expression into a list of atoms. They are defined by:
␈↓ ↓H␈↓␈↓ βp␈↓↓x = y ← x ␈↓αeq␈↓↓ y ∨ [¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y ∧ ␈↓αa|␈↓↓x = ␈↓αa|␈↓↓y ∧ ␈↓αd|␈↓↓x = ␈↓αd|␈↓↓y]␈↓,
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ∧y␈↓↓flatten[x] ← flat[x, ␈↓¬NIL␈↓↓] ␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ βy␈↓↓flat[x,y] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x.y ␈↓αelse␈↓↓ flat[␈↓αa|␈↓↓x,flat[␈↓αd|␈↓↓x,y]]␈↓.
␈↓ ↓H␈↓The␈α
kind␈αof␈α
double␈αrecursion␈α
used␈αin␈α
␈↓↓flatten␈↓␈αis␈α
often␈αuseful.␈α
Another␈αprogram␈α
which␈αcomputes
␈↓ ↓H␈↓the same function as ␈↓↓flatten␈↓ is
␈↓ ↓H␈↓␈↓ βi␈↓↓fringe x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <x> ␈↓αelse␈↓↓ fringe ␈↓αa|␈↓↓x * fringe ␈↓αd|␈↓↓x␈↓,
␈↓ ↓H␈↓which␈α∪is␈α∀a␈α∪bit␈α∪easier␈α∀to␈α∪follow␈α∪although␈α∀less␈α∪efficient,␈α∪because␈α∀the␈α∪␈↓↓append␈↓␈α∀function␈α∪copies
␈↓ ↓H␈↓unnecessarily.
␈↓ ↓H␈↓α␈↓ ε
Exercises
␈↓ ↓H␈↓ 1.␈α⊃Write␈α⊃a␈α⊃predicate␈α⊃to␈α⊃tell␈α⊃whether␈α⊃a␈α⊃given␈α⊃atom␈α⊃occurs␈α⊃in␈α⊃a␈α⊃given␈α⊃S-expression,␈α⊃e.g.
␈↓ ↓H␈↓␈↓↓occur[␈↓¬B, ␈↓↓␈↓¬((A.B).C)␈↓↓] = ␈↓¬T␈↓↓␈↓.
␈↓ ↓H␈↓ 2. Write a predicate to tell how many times a given atom occurs in an S-expression.
␈↓ ↓H␈↓ 3.␈α∞ Write␈α∞a␈α∞function␈α∞to␈α∞make␈α∞a␈α∞list␈α∞without␈α∞duplications␈α∞of␈α∞the␈α∞atoms␈α∞occurring␈α∞in␈α∂an␈α∞S-
␈↓ ↓H␈↓expression.
␈↓ ↓H␈↓ 4.␈α∞Write␈α∞a␈α∞function␈α∞to␈α∞make␈α∞a␈α∞list␈α∞of␈α∞all␈α∞atoms␈α∞that␈α∞occur␈α∞more␈α∞than␈α∞once␈α∞in␈α∞a␈α∞given␈α∞S-
␈↓ ↓H␈↓expression paired with their multiplicities.
␈↓ ↓H␈↓ 5.␈α∂Write␈α∂a␈α∞predicate␈α∂to␈α∂tell␈α∂whether␈α∞an␈α∂S-expression␈α∂has␈α∂more␈α∞than␈α∂one␈α∂occurrence␈α∂of␈α∞a
␈↓ ↓H␈↓given S-expression as a sub-expression.
␈↓ ↓H␈↓4. ␈↓αOther structural recursions.␈↓
␈↓ ↓H␈↓ When␈α
S-expressions␈α
are␈α
used␈α
to␈α
represent␈α
a␈α
class␈α
of␈α
expressions␈α
that␈α
are␈αdefined␈α
inductively
␈↓ ↓H␈↓then␈α∞functions␈α∞of␈α∞these␈α∞expressions␈α∞often␈α∞have␈α∞a␈α∞recursive␈α∞form␈α∞closely␈α∞related␈α∞to␈α∞the␈α∞inductive
␈↓ ↓H␈↓definition␈α∂of␈α⊂the␈α∂expressions.␈α⊂ For␈α∂example␈α⊂the␈α∂arithmetic␈α⊂interpreter␈α∂␈↓↓numval␈↓␈α⊂(I.11.3)␈α∂computes
␈↓ ↓H␈↓directly␈αthe␈αvalue␈αfor␈αconstants␈αand␈αvariables␈αand␈αfor␈αsums␈αand␈αproducts␈αit␈αcomputes␈αthe␈αvalue␈α
in
␈↓ ↓H␈↓terms␈αof␈α
the␈αvalues␈αof␈α
subexpressions.␈α The␈αfunction␈α
␈↓↓diff␈↓␈α(I.9.4)␈αwhich␈α
symbolically␈αdifferentiates
␈↓ ↓H␈↓32␈↓ ¬}Chapter II␈↓ H
␈↓ ↓H␈↓the␈α⊃same␈α⊃class␈α⊃of␈α∩expressions␈α⊃has␈α⊃a␈α⊃similar␈α⊃recursive␈α∩structure␈α⊃in␈α⊃that␈α⊃it␈α⊃knows␈α∩the␈α⊃answers
␈↓ ↓H␈↓immediately␈α∀for␈α∀constants␈α∀and␈α∀variables␈α∀and␈α∀for␈α∪sums␈α∀and␈α∀product␈α∀it␈α∀uses␈α∀the␈α∀results␈α∪for
␈↓ ↓H␈↓subexpressions␈α∞to␈α∞obtain␈α∞the␈α∞final␈α∞answer.␈α∞ A␈α∞more␈α∞complicated␈α∞example␈α∞of␈α∞an␈α∞interpreter␈α
with
␈↓ ↓H␈↓recursive structure based on the expression language structure is the LISP interpreter ␈↓↓eval␈↓ (I.12.1).
␈↓ ↓H␈↓5. ␈↓αTree search recursion.␈↓
␈↓ ↓H␈↓ Many␈α∂programs␈α∂use␈α∂the␈α⊂technique␈α∂of␈α∂exploring␈α∂a␈α⊂"search␈α∂space"␈α∂of␈α∂possible␈α⊂solutions␈α∂in
␈↓ ↓H␈↓order␈αto␈α
solve␈αa␈α
problem.␈α It␈α
is␈αoften␈α
convenient␈αto␈α
represent␈αthe␈α
search␈αspace␈α
as␈αa␈α
tree␈αstructure
␈↓ ↓H␈↓with␈αa␈αstarting␈αposition␈αand␈αsome␈αmethod␈αof␈α
getting␈αto␈αsuccessor␈αpositions.␈α In␈αthis␈αcase␈αthe␈α
search
␈↓ ↓H␈↓can␈α
be␈αcontrolled␈α
by␈α
some␈αstandard␈α
algorithm␈α
for␈αvisiting␈α
all␈α
nodes␈αof␈α
a␈α
tree.␈α One␈α
such␈αmethod␈α
is
␈↓ ↓H␈↓the␈α
depth␈α
first␈α
search.␈α
It␈α
consists␈α
of␈α∞taking␈α
the␈α
first␈α
(in␈α
some␈α
fixed␈α
ordering)␈α
untried␈α∞branch␈α
at
␈↓ ↓H␈↓each␈αlevel␈αuntil␈αa␈αleaf␈α(terminal␈αposition)␈αis␈α
reached␈αor␈αno␈αmore␈αbranches␈αare␈αleft.␈α In␈α
either␈αcase
␈↓ ↓H␈↓pop up a level and continue the search until all nodes have been visited.
␈↓ ↓H␈↓ Simple␈αS-expression␈αrecursion␈αcan␈αbe␈αregarded␈αas␈αa␈αspecial␈αcase␈αof␈αdepth␈αfirst␈αrecursion.␈α It
␈↓ ↓H␈↓is␈αspecial␈αin␈αthat␈αthere␈α
are␈αexactly␈αtwo␈αbranches,␈αbut␈αeven␈α
more␈αimportant,␈αthe␈αtree␈αis␈αthe␈α
tree␈αof
␈↓ ↓H␈↓parts of the S-expression and is present at the beginning of the calculation.
␈↓ ↓H␈↓ In␈α
the␈α
general␈α
case␈α
of␈α
tree␈α
search␈α∞recursion,␈α
the␈α
tree␈α
is␈α
generated␈α
by␈α
a␈α∞␈↓↓successors␈↓␈α
function.
␈↓ ↓H␈↓We␈α∞can␈α∞describe␈α∞the␈α∞tree␈α∂search␈α∞in␈α∞a␈α∞problem␈α∞independent␈α∂way␈α∞by␈α∞the␈α∞general␈α∞depth␈α∂first␈α∞tree
␈↓ ↓H␈↓search␈α
function␈α
␈↓↓search.␈↓␈α It␈α
can␈α
be␈α
used␈αto␈α
search␈α
specific␈α
trees␈αof␈α
possibilities␈α
by␈α
defining␈αthe␈α
three
␈↓ ↓H␈↓auxiliary functions ␈↓↓successors,␈↓ ␈↓↓ter,␈↓ and ␈↓↓lose␈↓ for the particular problem. We have
␈↓ ↓H␈↓5.1) ␈↓ αU␈↓↓search p ← ␈↓αif␈↓↓ lose p ␈↓αthen␈↓↓ ␈↓¬LOSE ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ter p ␈↓αthen␈↓↓ p ␈↓αelse␈↓↓ searchlis[successors p]␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓5.2) ␈↓ α␈↓↓searchlis u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬LOSE ␈↓↓ ␈↓αelse␈↓↓ {search ␈↓αa|␈↓↓u}[λx: ␈↓αif␈↓↓ x = ␈↓¬LOSE ␈↓↓␈↓αthen␈↓↓ searchlis ␈↓αd|␈↓↓u ␈↓αelse␈↓↓ x]␈↓.
␈↓ ↓H␈↓In␈α
the␈α
applications,␈α
we␈α
start␈α
with␈αa␈α
position␈α
␈↓↓p0,␈↓␈α
and␈α
we␈α
are␈αlooking␈α
for␈α
a␈α
win␈α
in␈α
the␈αsuccessor␈α
tree
␈↓ ↓H␈↓of␈α
␈↓↓p0.␈↓␈α Certain␈α
positions␈αlose␈α
and␈α
there␈αis␈α
no␈αpoint␈α
looking␈α
at␈αtheir␈α
successors.␈α This␈α
is␈αdecided␈α
by
␈↓ ↓H␈↓the␈α
predicate␈α∞␈↓↓lose.␈↓␈α
A␈α
position␈α∞is␈α
a␈α
win␈α∞if␈α
it␈α
doesn't␈α∞lose␈α
and␈α
it␈α∞satisfies␈α
the␈α
predicate␈α∞␈↓↓ter.␈↓␈α
The
␈↓ ↓H␈↓successors␈α∞of␈α∞a␈α∞position␈α∂are␈α∞given␈α∞by␈α∞the␈α∂function␈α∞␈↓↓successors,␈↓␈α∞and␈α∞the␈α∂value␈α∞of␈α∞␈↓↓search␈α∞p␈↓␈α∂is␈α∞the
␈↓ ↓H␈↓winning␈αposition.␈α No␈αnon-losing␈αposition␈αshould␈αhave␈α
the␈αname␈α␈↓¬LOSE␈α␈↓or␈αthe␈αfunction␈αwon't␈α
work
␈↓ ↓H␈↓properly.
␈↓ ↓H␈↓ Our␈α∂first␈α∂application␈α∞is␈α∂finding␈α∂a␈α∞path␈α∂from␈α∂an␈α∂initial␈α∞node␈α∂to␈α∂a␈α∞final␈α∂node␈α∂in␈α∂a␈α∞graph
␈↓ ↓H␈↓represented␈α
by␈α
a␈α
list␈α
structure␈α
as␈α
described␈α
in␈α∞chapter␈α
I.␈α
A␈α
position␈α
is␈α
a␈α
path␈α
starting␈α∞from␈α
the
␈↓ ↓H␈↓initial␈αnode␈αand␈αcontinuing␈αto␈αsome␈αintermediate␈αnode␈αand␈αis␈αrepresented␈αby␈αa␈αlist␈αof␈αits␈αnodes␈αin
␈↓ ↓H␈↓reverse order. The three functions for this application are
␈↓ ↓H␈↓5.3)␈↓ βx␈↓↓lose p ← ␈↓αa|␈↓↓p ε ␈↓αd|␈↓↓p , ␈↓
␈↓ ↓H␈↓5.4)␈↓ βx␈↓↓ter p ← [␈↓αa|␈↓↓p = final] , ␈↓
␈↓ ↓H␈↓5.5)␈↓ βx␈↓↓successors p ← mapcar[λx: x . p, ␈↓αd|␈↓↓assoc[␈↓αa|␈↓↓p, graph]]␈↓.
␈↓ ↓H␈↓The␈αnode␈αwe␈αare␈αproposing␈αto␈αvisit␈αnext␈αloses␈αif␈αit␈αis␈αalready␈αin␈αthe␈αpath,␈αas␈αthat␈αmeans␈αwe␈αhave
␈↓ ↓H␈↓␈↓ ¬}Chapter II␈↓ *33
␈↓ ↓H␈↓been␈α∞here␈α∞before.␈α∞ The␈α
successors␈α∞to␈α∞a␈α∞position␈α∞are␈α
all␈α∞of␈α∞those␈α∞positions␈α∞immediately␈α
reachable
␈↓ ↓H␈↓from␈α
the␈αcurrent␈α
node.␈α
In␈αour␈α
list␈αnotation␈α
for␈α
a␈αgraph␈α
the␈α
immediately␈αreachable␈α
nodes␈αare␈α
those
␈↓ ↓H␈↓on␈α
the␈α
list␈α
associated␈α
with␈α
the␈α
current␈α
node␈α
in␈α
␈↓↓graph.␈↓␈α
A␈α
position␈α
is␈α
terminal␈α
if␈α
the␈α
current␈α
node␈α
is
␈↓ ↓H␈↓the desired goal ␈↓↓final.␈↓
␈↓ ↓H␈↓ An␈α∂more␈α∂complicated␈α∂example␈α∂is␈α∂the␈α∂so-called␈α∂␈↓αInstant␈α∂Insanity␈↓␈α∂puzzle.␈α∂ There␈α⊂are␈α∂four
␈↓ ↓H␈↓cubical␈αblocks,␈αand␈αeach␈αface␈αof␈αeach␈αblock␈αis␈αcolored␈αwith␈αone␈αof␈αfour␈αcolors.␈α The␈αobject␈αof␈αthe
␈↓ ↓H␈↓puzzle␈αis␈αto␈αbuild␈αa␈αtower␈αof␈αall␈αfour␈αblocks␈αsuch␈αthat␈αeach␈αvertical␈αface␈αof␈αthe␈αtower␈αinvolves␈αall
␈↓ ↓H␈↓four␈α
colors.␈α
In␈α
order␈α
to␈α
use␈α
the␈α
above␈α
defined␈α
function␈α
␈↓↓search␈↓␈α
for␈α
this␈α
purpose,␈α
we␈α
must␈αdefine
␈↓ ↓H␈↓the␈α∂representation␈α∞of␈α∂positions␈α∞and␈α∂give␈α∂the␈α∞functions␈α∂␈↓↓lose,␈↓␈α∞␈↓↓ter,␈↓␈α∂and␈α∞␈↓↓successors.␈↓␈α∂ A␈α∂position␈α∞is
␈↓ ↓H␈↓represented␈αby␈αa␈αlist␈αof␈αlists␈α-␈αone␈αfor␈αeach␈αface␈αof␈αthe␈αtower.␈α Each␈αsublist␈αis␈αthe␈αlist␈αof␈αcolors␈αof
␈↓ ↓H␈↓the␈α∞faces␈α∞of␈α∞the␈α∞blocks␈α∞showing␈α∞in␈α∞that␈α∂face.␈α∞ Initially␈α∞the␈α∞tower␈α∞is␈α∞empty.␈α∞ The␈α∞successors␈α∂of␈α∞a
␈↓ ↓H␈↓position␈αare␈αobtained␈αby␈αadding␈αthe␈αnext␈αunused␈αblock␈αin␈αall␈αpossible␈αorientations,␈αeg.␈αby␈αadding
␈↓ ↓H␈↓the␈αappropriate␈αcolor␈αto␈αeach␈αof␈αthe␈αfour␈αsublists.␈α We␈αshall␈αassume␈αthat␈αthe␈αblocks␈αare␈αdescribed
␈↓ ↓H␈↓in␈α∞the␈α∞following␈α∂longwinded␈α∞but␈α∞convenient␈α∞way.␈α∂ (We'll␈α∞take␈α∞up␈α∞precomputing␈α∂this␈α∞description
␈↓ ↓H␈↓later.) ␈αFor␈αeach␈αblock␈αthere␈αis␈αa␈αlist␈αof␈αthe␈α24␈αorientations␈αof␈αthe␈αblock␈αwhere␈αeach␈αorientation␈αis
␈↓ ↓H␈↓described␈α∪as␈α∩a␈α∪list␈α∩of␈α∪the␈α∩colors␈α∪around␈α∪the␈α∩vertical␈α∪faces␈α∩of␈α∪the␈α∩block␈α∪when␈α∩it␈α∪is␈α∪in␈α∩that
␈↓ ↓H␈↓orientation. Thus the puzzle is described by a list of lists of lists which we shall call ␈↓↓puzz.␈↓
␈↓ ↓H␈↓ We now have
␈↓ ↓H␈↓5.6)␈↓ αx␈↓↓lose p ← orlis[λu: ␈↓αa|␈↓↓u ε ␈↓αd|␈↓↓u, p],␈↓
␈↓ ↓H␈↓5.7)␈↓ αx␈↓↓ ter p ← [length ␈↓αa|␈↓↓p = 4], ␈↓
␈↓ ↓H␈↓5.8)␈↓ αx␈↓↓successors p ← mapcar[λx: mapcar2[λyz: z.y, p, x], ␈↓αa|␈↓↓nth[puzz, 1 + length ␈↓αa|␈↓↓p]]␈↓
␈↓ ↓H␈↓where the functions ␈↓↓mapcar2␈↓ and ␈↓↓nth␈↓ are given by
␈↓ ↓H␈↓5.9)␈↓ αx␈↓↓mapcar2[f, u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ f[␈↓αa|␈↓↓u, ␈↓αa|␈↓↓v] . mapcar2[f, ␈↓αd|␈↓↓u, ␈↓αd|␈↓↓v]␈↓.
␈↓ ↓H␈↓5.10)␈↓ αx␈↓↓nth[u, n] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ∨ n=1 ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ nth[␈↓αd|␈↓↓u, n-1]␈↓.
␈↓ ↓H␈↓␈↓↓nth␈↓␈α⊃picks␈α⊃out␈α⊂the␈α⊃list␈α⊃repesentint␈α⊃the␈α⊂next␈α⊃block␈α⊃in␈α⊃␈↓↓puzz.␈↓␈α⊂ The␈α⊃outer␈α⊃mapping␈α⊃functon␈α⊂runs
␈↓ ↓H␈↓through␈αthe␈αlist␈αof␈αorientations␈αreturned␈αby␈α␈↓↓nth␈↓␈αand␈αmakes␈αa␈αnew␈αposition␈αfor␈αeach␈αone.␈α This␈αis
␈↓ ↓H␈↓done␈αby␈α
the␈αinner␈αmapping␈α
function␈αwhich␈αruns␈α
simultaneously␈αthrough␈αthe␈α
list␈αtower␈α
faces␈αand
␈↓ ↓H␈↓block faces, putting the block face on the tower face.
␈↓ ↓H␈↓A solution to the puzzle is obtained by assigning a value to ␈↓↓puzz␈↓ and evaluating ␈↓↓search[p0]␈↓ with
␈↓ ↓H␈↓␈↓ ¬∨␈↓↓ p0 = ␈↓¬(NIL NIL NIL NIL)␈↓↓. ␈↓
␈↓ ↓H␈↓ Obtaining␈α⊂␈↓↓puzz␈↓␈α⊂in␈α⊂the␈α⊂desired␈α⊂form␈α⊂is␈α⊂as␈α⊂complicated␈α⊂a␈α⊂computation␈α⊂as␈α⊂the␈α⊃actual␈α⊂tree
␈↓ ↓H␈↓search.␈α∂ It␈α⊂can␈α∂be␈α∂conveniently␈α⊂done␈α∂by␈α∂a␈α⊂sequence␈α∂of␈α∂assignment␈α⊂statements␈α∂starting␈α⊂with␈α∂the
␈↓ ↓H␈↓following description of the blocks:
␈↓ ↓H␈↓␈↓ β␈↓↓puzz1 ← ␈↓¬((G B B W R G) (G G B G W R) (G W W R B R) (G G R B W W))␈↓↓␈↓.
␈↓ ↓H␈↓Here␈αeach␈αblock␈αis␈αrepresented␈αby␈αa␈αlist␈αof␈αthe␈αcolors␈αof␈αthe␈αfaces␈αstarting␈αwith␈αthe␈αtop␈αface,␈αgoing
␈↓ ↓H␈↓around the sides in a clockwise direction and finishing with the bottom face.
␈↓ ↓H␈↓ We␈αneed␈αto␈α
go␈αfrom␈αthis␈α
description␈αof␈αthe␈α
blocks␈αto␈αa␈α
list␈αof␈αthe␈α
possible␈αcycles␈αof␈αcolors␈α
on
␈↓ ↓H␈↓the␈αvertical␈αfaces␈αfor␈αthe␈α24␈αorientations␈αof␈αthe␈αblock.␈α This␈αnot␈αeasy,␈αbecause␈αthe␈αorder␈αin␈αwhich
␈↓ ↓H␈↓34␈↓ ¬}Chapter II␈↓ H
␈↓ ↓H␈↓we␈α
have␈α
given␈αthe␈α
colors␈α
is␈α
not␈αinvariant␈α
under␈α
rotations␈α
of␈αthe␈α
block.␈α
An␈α
easy␈αway␈α
out␈α
is␈αto␈α
start
␈↓ ↓H␈↓with␈αa␈αblock␈αwhose␈αfaces␈αare␈αassigned␈αthe␈αnumbers␈α1␈αthru␈α6␈αstarting␈αwith␈αthe␈αtop,␈αgoing␈α
clockwise
␈↓ ↓H␈↓around␈αthe␈αsides␈αand␈αfinishing␈αwith␈αthe␈αbottom.␈α We␈αwrite␈αdown␈αone␈αcycle␈αof␈αside␈αcolors␈αfor␈αeach
␈↓ ↓H␈↓choice␈α∩of␈α⊃the␈α∩face␈α⊃put␈α∩on␈α⊃top␈α∩and␈α⊃get␈α∩the␈α⊃list␈α∩of␈α⊃all␈α∩24␈α⊃cycles␈α∩by␈α⊃appending␈α∩the␈α∩results␈α⊃of
␈↓ ↓H␈↓generating the cyclic permutations of the cycles. All this is accomplished by the assignment,
␈↓ ↓H␈↓␈↓ β>␈↓↓puzz2 ← cycles[␈↓¬(2 3 4 5)␈↓↓] * cycles[␈↓¬(2 5 4 3)␈↓↓] * cycles[␈↓¬(1 2 6 4)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ βH␈↓↓ * cycles[␈↓¬(1 4 6 2)␈↓↓] * cycles[␈↓¬(1 3 6 5)␈↓↓] * cycles[␈↓¬(1 5 6 3)␈↓↓]␈↓,
␈↓ ↓H␈↓where the function ␈↓↓cycles␈↓ is defined by
␈↓ ↓H␈↓5.11) ␈↓ βY␈↓↓ cycles u ← maplist[λv: v * upto[u, v], u] ␈↓
␈↓ ↓H␈↓5.12) ␈↓ βi␈↓↓upto[u, v] ← ␈↓αif␈↓↓ v ␈↓αeq␈↓↓ u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . upto[␈↓αd|␈↓↓u, v]␈↓.
␈↓ ↓H␈↓ Next␈αwe␈α
create␈αa␈αlist␈α
of␈αsubstitution␈αlists,␈α
one␈αfor␈αeach␈α
block,␈αexpressing␈αthe␈α
colors␈αof␈αthe␈α
six
␈↓ ↓H␈↓numbered faces. This is done by the assignment
␈↓ ↓H␈↓␈↓ β9␈↓↓puzz3 ← mapcar[λv: mapcar2[␈↓¬CONS, ␈↓↓␈↓¬(1 2 3 4 5 6)␈↓↓, v], puzz1]␈↓.
␈↓ ↓H␈↓We use these substitutions to get for each block the list of 24 orientations of the block. Thus
␈↓ ↓H␈↓␈↓ β=␈↓↓puzz4 ← mapcar[λs: mapcar[λx: ␈↓αd|␈↓↓assoc[x,s], puzz2], puzz3]␈↓.
␈↓ ↓H␈↓␈↓↓puzz4␈↓␈αhas␈αall␈α24␈αorientations␈αof␈αthe␈αfirst␈αblock␈αwhile␈αfor␈αsymmetry␈αreasons␈αwe␈αneed␈αonly␈αconsider
␈↓ ↓H␈↓three as distinct, say the first, ninth, and seventeenth. So we finally get
␈↓ ↓H␈↓␈↓ αX␈↓↓puzz ← <␈↓αa|␈↓↓nth[␈↓αa|␈↓↓puzz4, 1], ␈↓αa|␈↓↓nth[␈↓αa|␈↓↓puzz4, 9], ␈↓αa|␈↓↓nth[␈↓αa|␈↓↓puzz4, 17]> . ␈↓αd|␈↓↓puzz4␈↓.
␈↓ ↓H␈↓The program when compiled runs about 11 seconds on the KA-10.
␈↓ ↓H␈↓ A␈αmore␈αsophisticated␈αrepresentation␈αof␈αface␈α
cycles␈αand␈αpartial␈αtowers␈αmakes␈αa␈αfactor␈α
of␈αten
␈↓ ↓H␈↓speedup␈αwithout␈αchanging␈αthe␈αbasic␈αsearch␈αalgorithm.␈α Here␈αa␈αface␈αcycle␈αis␈αrepresented␈αby␈α16␈αbits
␈↓ ↓H␈↓in␈αa␈αword,␈αfour␈αfor␈αeach␈αface,␈αa␈αparticular␈αone␈αof␈αwhich␈αbeing␈αturned␈αon␈αtells␈αus␈αthe␈αcolor␈αof␈αthe
␈↓ ↓H␈↓face.␈α If␈αwe␈α␈↓αor␈↓␈αthese␈αwords␈αtogether␈αfor␈αthe␈αblocks␈αin␈αa␈αpartial␈αtower␈αwe␈αget␈αa␈αword␈αwhich␈αtells␈αus
␈↓ ↓H␈↓for␈α∞each␈α∂face␈α∞of␈α∂the␈α∞tower␈α∂what␈α∞colors␈α∂have␈α∞been␈α∂used.␈α∞ A␈α∂particular␈α∞face␈α∂cycle␈α∞from␈α∂the␈α∞next
␈↓ ↓H␈↓block␈α
can␈α
be␈α
added␈α
to␈α
the␈α
tower␈α
if␈α
the␈α
␈↓αand␈↓␈α
of␈α
its␈α
word␈α
with␈α
the␈α
word␈α
representing␈α
the␈α
tower␈αis
␈↓ ↓H␈↓zero.␈α We␈αrepresent␈αa␈αposition␈αby␈αa␈αlist␈αof␈αwords␈αrepresenting␈αits␈αpartial␈αtowers␈αwith␈α0␈αas␈αthe␈αlast
␈↓ ↓H␈↓element␈α∞and␈α∞the␈α∞highest␈α∞partial␈α∞tower␈α∞as␈α∞the␈α∞first␈α∞element.␈α∞ The␈α∞virtue␈α∞of␈α∞this␈α∞representation␈α
is
␈↓ ↓H␈↓that␈α
it␈α
makes␈α
the␈α
description␈α∞of␈α
the␈α
algorithm␈α
short.␈α
The␈α∞initial␈α
position␈α
is␈α
␈↓¬(0)␈↓.␈α
The␈α∞new␈α
␈↓↓puzz␈↓
␈↓ ↓H␈↓can be formed from the old one by the assignment
␈↓ ↓H␈↓␈↓ ∧,␈↓↓puzza ← mapcar[λu: mapcar[zap, u], puzz]␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓5.13)␈↓ β8␈↓↓zap v ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓v ␈↓αthen␈↓↓ 0 ␈↓αelse␈↓↓ poo ␈↓αa|␈↓↓v + 16 ␈↓π*␈↓↓ zap ␈↓αd|␈↓↓v␈↓
␈↓ ↓H␈↓5.14)␈↓ β8␈↓↓poo x ← ␈↓αif␈↓↓ x = ␈↓¬R ␈↓↓␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ if x = ␈↓¬W ␈↓↓␈↓αthen␈↓↓ 2 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ x = ␈↓¬G ␈↓↓␈↓αthen␈↓↓ 4 ␈↓αelse␈↓↓ 8␈↓.
␈↓ ↓H␈↓Now we need the new functions ␈↓↓lose,␈↓ ␈↓↓ter,␈↓ and ␈↓↓successors.␈↓ These are
␈↓ ↓H␈↓␈↓ ¬}Chapter II␈↓ *35
␈↓ ↓H␈↓5.15)␈↓ β8␈↓↓lose p ← ␈↓αfalse␈↓↓ , ␈↓
␈↓ ↓H␈↓5.16)␈↓ β8␈↓↓ter p ← [length p = 5]␈↓,
␈↓ ↓H␈↓5.17)␈↓ β8␈↓↓successors p ← mapchoose[[λx: ␈↓αa|␈↓↓p ␈↓¬and ␈↓↓x = 0], ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ [λx: ␈↓αa|␈↓↓p ␈↓¬or ␈↓↓x] . p, ␈↓αa|␈↓↓nth[puzza, length p]]␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ β8␈↓↓mapchoose[pred, fn, u] ← ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓5.18)␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ pred ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ fn[␈↓αa|␈↓↓u] . mapchoose[pred, fn, ␈↓αd|␈↓↓u] ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ mapchoose[pred, fn, ␈↓αd|␈↓↓u]␈↓.
␈↓ ↓H␈↓␈↓↓lose␈↓␈αis␈αtrivial,␈αbecause␈αthe␈α␈↓↓mapchoose␈↓␈αis␈αused␈αto␈αmake␈αsure␈αthat␈αonly␈αnon-losing␈αnew␈αpositions␈αare
␈↓ ↓H␈↓generated␈α∂by␈α∂␈↓↓successors.␈↓␈α∂This␈α∂version␈α⊂runs␈α∂in␈α∂a␈α∂little␈α∂less␈α⊂than␈α∂one␈α∂second␈α∂on␈α∂the␈α⊂KA-10.␈α∂ A
␈↓ ↓H␈↓greater␈α⊃speedup␈α⊂can␈α⊃be␈α⊂made␈α⊃by␈α⊃the␈α⊂application␈α⊃of␈α⊂some␈α⊃mathematics.␈α⊂ In␈α⊃fact,␈α⊃with␈α⊂enough
␈↓ ↓H␈↓mathematics, extensive tree search is unnecessary in this problem.
␈↓ ↓H␈↓ ␈↓↓search␈↓␈α∞is␈α∞used␈α
when␈α∞we␈α∞want␈α
to␈α∞search␈α∞a␈α∞tree␈α
of␈α∞possibilities␈α∞for␈α
a␈α∞solution␈α∞to␈α∞a␈α
problem.
␈↓ ↓H␈↓Naturally␈α∞we␈α∞can␈α∞do␈α∞other␈α∞things␈α∞with␈α∞tree␈α∞search␈α∞recursion␈α∞than␈α∞just␈α∞search.␈α∞ For␈α∞example␈α
we
␈↓ ↓H␈↓may␈α
want␈αto␈α
find␈αall␈α
solutions␈α
to␈αa␈α
problem.␈α This␈α
can␈αbe␈α
done␈α
with␈αa␈α
function␈α␈↓↓allsol␈↓␈α
that␈αuses␈α
the
␈↓ ↓H␈↓same ␈↓↓lose,␈↓ ␈↓↓ter,␈↓ and ␈↓↓successors␈↓ functions as does ␈↓↓search.␈↓ The simplest way to write ␈↓↓allsol␈↓ is
␈↓ ↓H␈↓5.19) ␈↓ α'␈↓↓allsol p ← ␈↓αif␈↓↓ lose p ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ter p ␈↓αthen␈↓↓ <p> ␈↓αelse␈↓↓ mapapp[allsol, successors p]␈↓,
␈↓ ↓H␈↓where
␈↓ ↓H␈↓5.20) ␈↓ β$␈↓↓mapapp[fn, u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ fn[␈↓αa|␈↓↓u] * mappap[fn, ␈↓αd|␈↓↓u]␈↓.
␈↓ ↓H␈↓This␈αform␈αof␈αthe␈αfunction␈αis␈αsomewhat␈αinefficient␈αbecause␈αof␈αall␈αthe␈α␈↓↓append␈↓ing.␈α A␈αmore␈αefficient
␈↓ ↓H␈↓form uses an auxiliary function as follows:
␈↓ ↓H␈↓␈↓ β8␈↓↓allsol p ← allsola[p, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓allsola[p, found] ← ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αif␈↓↓ lose p ␈↓αthen␈↓↓ found␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ter p ␈↓αthen␈↓↓ p . found␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ allsolb[successors p, found]␈↓,
␈↓ ↓H␈↓␈↓ β8␈↓↓allsolb[u, found] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ found ␈↓αelse␈↓↓ allsolb[␈↓αd|␈↓↓u, allsola[␈↓αa|␈↓↓u, found]]␈↓.
␈↓ ↓H␈↓The␈α⊃recursive␈α⊂program␈α⊃structure␈α⊂that␈α⊃arises␈α⊂here␈α⊃is␈α⊃common␈α⊂when␈α⊃a␈α⊂list␈α⊃is␈α⊂to␈α⊃be␈α⊃formed␈α⊂by
␈↓ ↓H␈↓recurring␈α∀through␈α∃a␈α∀list␈α∀structure.␈α∃ For␈α∀an␈α∃introduction␈α∀to␈α∀the␈α∃general␈α∀topic␈α∃of␈α∀searching
␈↓ ↓H␈↓techniques see Nilsson[1971] for a more detailed discussion of searching techniques.
␈↓ ↓H␈↓6. ␈↓αGame trees.␈↓
␈↓ ↓H␈↓ The␈α⊂positions␈α∂that␈α⊂can␈α∂be␈α⊂reached␈α∂from␈α⊂an␈α⊂initial␈α∂position␈α⊂in␈α∂a␈α⊂game␈α∂form␈α⊂a␈α⊂tree,␈α∂and
␈↓ ↓H␈↓36␈↓ ¬}Chapter II␈↓ H
␈↓ ↓H␈↓deciding␈α
what␈αmove␈α
to␈α
make␈αoften␈α
involves␈α
searching␈αthis␈α
tree.␈α
However,␈αgame␈α
trees␈αare␈α
searched
␈↓ ↓H␈↓in␈αa␈αdifferent␈αway␈αthan␈αthe␈αtrees␈αwe␈αhave␈αlooked␈αat␈αpreviously,␈αbecause␈αthe␈αopposing␈αinterests␈αof
␈↓ ↓H␈↓the␈αplayers␈αmakes␈αit␈αnot␈αa␈αsearch␈αfor␈αa␈αjoint␈αline␈αof␈αplay␈αthat␈αwill␈αlead␈αto␈αthe␈αfirst␈αplayer␈α
winning,
␈↓ ↓H␈↓but␈αrather␈αa␈αsearch␈αfor␈αa␈αstrategy␈αthat␈αwill␈αlead␈αto␈αa␈αwin␈αregardless␈αof␈αwhat␈αthe␈αother␈αplayer␈αdoes.
␈↓ ↓H␈↓In␈αthis␈αsection␈αwe␈αdiscuss␈αtwo␈αmethods␈αfor␈αsearching␈αa␈αgame␈αtree.␈α The␈αfirst␈αis␈αa␈αsimple␈αminimax
␈↓ ↓H␈↓search␈α∞which␈α∞examines␈α∞all␈α∞lines␈α∞of␈α∞play␈α∞before␈α
making␈α∞a␈α∞decision.␈α∞ The␈α∞second␈α∞makes␈α∞use␈α∞of␈α
a
␈↓ ↓H␈↓heuristic␈α
known␈αas␈α
the␈ααβ-heuristic␈α
to␈αprune␈α
the␈αsearch␈α
space.␈α In␈α
both␈αcases␈α
the␈α
basic␈αrecursive
␈↓ ↓H␈↓structure␈α
of␈α
the␈αcomputation␈α
is␈α
presented␈αin␈α
a␈α
game␈αindependent␈α
fashion␈α
by␈αletting␈α
the␈α
game␈αbe
␈↓ ↓H␈↓characterized␈αby␈αa␈αcollection␈αof␈αfunctions␈αwhich␈αsatisfy␈αsome␈αgeneral␈αconditions.␈α The␈αgame␈αof␈α2-
␈↓ ↓H␈↓dimensional␈α∂Tic Tac Toe␈α∂will␈α∂be␈α∞used␈α∂as␈α∂an␈α∂example␈α∞application␈α∂of␈α∂these␈α∂game␈α∂tree␈α∞searching
␈↓ ↓H␈↓techniques.
␈↓ ↓H␈↓ In␈α∞the␈α
simplest␈α∞situation␈α
the␈α∞game␈α∞is␈α
characterized␈α∞by␈α
a␈α∞function␈α
␈↓↓successors␈↓␈α∞that␈α∞gives␈α
the
␈↓ ↓H␈↓positions␈α∂that␈α∂can␈α∂be␈α∂reached␈α∂in␈α∂one␈α∂move,␈α∂a␈α∂predicate␈α∂␈↓↓ter␈↓␈α∂that␈α∂tells␈α∂when␈α∂a␈α∂position␈α∂is␈α∂to␈α∂be
␈↓ ↓H␈↓regarded␈α⊗as␈α⊗terminal␈α⊗for␈α⊗the␈α⊗given␈α⊗analysis,␈α⊗and␈α⊗a␈α⊗function␈α⊗␈↓↓imval␈↓␈α⊗that␈α⊗gives␈α⊗a␈α∃number
␈↓ ↓H␈↓approximating␈α
the␈α
value␈αof␈α
a␈α
terminal␈αposition␈α
to␈α
one␈α
of␈αthe␈α
players.␈α
We␈αshall␈α
call␈α
this␈αplayer␈α
the
␈↓ ↓H␈↓maximizing␈α
player␈α
and␈α
his␈α
opponent␈α
the␈αminimizing␈α
player.␈α
Usually,␈α
the␈α
numerical␈α
values␈αarise,
␈↓ ↓H␈↓because␈α∂the␈α∂search␈α∂cannot␈α∂be␈α∂carried␈α∂out␈α∂to␈α∂the␈α∂end␈α∂of␈α∂the␈α∂game,␈α∂and␈α∂the␈α∂analysis␈α⊂stops␈α∂with
␈↓ ↓H␈↓reasonably␈α∞static␈α∞positions␈α
that␈α∞can␈α∞be␈α∞evaluated␈α
by␈α∞some␈α∞rule.␈α
Naturally,␈α∞the␈α∞function␈α∞␈↓↓imval␈↓␈α
is
␈↓ ↓H␈↓chosen␈α
to␈α
be␈α
easy␈α
to␈α
calculate␈α
and␈α
to␈α
correlate␈α
well␈α
with␈α
the␈α
probability␈α
that␈α
the␈αmaximizing␈α
player
␈↓ ↓H␈↓can win the position.
␈↓ ↓H␈↓ Consider␈α
the␈α∞game␈α
of␈α
Tic␈α∞Tac␈α
Toe␈α
played␈α∞in␈α
2-dimensions.␈α
A␈α∞position␈α
is␈α∞determined␈α
by
␈↓ ↓H␈↓knowing␈α
which␈α
of␈α
the␈α
nine␈α
squares␈α
contain␈α
X's,␈α
O's,␈α
or␈α
blanks.␈α
The␈α
successors␈α
of␈α
a␈α
position␈α
in
␈↓ ↓H␈↓the␈αcase␈αthat␈αit␈αis␈αthe␈αX-players␈αturn␈αto␈αmove␈αare␈αall␈αthose␈αpositions␈αobtained␈αby␈αputting␈αan␈αX␈αin
␈↓ ↓H␈↓a␈αblank␈αsquare.␈α A␈αposition␈αis␈αterminal␈αif␈αthere␈αare␈αno␈αblank␈αsquares,␈αor␈αif␈αthere␈αare␈αthree␈αX's␈αor
␈↓ ↓H␈↓three␈α
O's␈α∞on␈α
a␈α
line␈α∞or␈α
diagonal.␈α∞ If␈α
we␈α
take␈α∞the␈α
X-player␈α∞to␈α
be␈α
the␈α∞maximizing␈α
player␈α∞then␈α
we
␈↓ ↓H␈↓could␈αassign␈αvalues␈αto␈αthe␈αterminal␈αpositions␈αas␈αfollows.␈α In␈αthe␈αcase␈αof␈αthree␈αX's␈αthe␈αvalue␈αis␈α1,␈α
in
␈↓ ↓H␈↓the case of three O's the value is -1 and otherwise it is 0.
␈↓ ↓H␈↓ The␈α
simple␈α
minimax␈α
rule␈α
for␈α
finding␈α
the␈αcorrect␈α
move␈α
in␈α
a␈α
position␈α
uses␈α
a␈α
pair␈αof␈α
programs
␈↓ ↓H␈↓␈↓↓(vmin, vmax)␈↓.␈α ␈↓↓vmax␈↓␈αgives␈αthe␈αvalue␈αof␈αa␈αposition␈αto␈αthe␈αmaximizing␈αplayer␈αby␈αusing␈α␈↓↓imval␈↓␈αif␈αthe
␈↓ ↓H␈↓position␈αis␈αterminal␈αand␈αtaking␈αthe␈αmax␈αof␈αthe␈αvalues␈αof␈αthe␈αsuccessor␈αpositions␈αto␈αthe␈α
minimizing
␈↓ ↓H␈↓player otherwise. ␈↓↓vmin␈↓ similiarly gives the value of a position to the minimizing player.
␈↓ ↓H␈↓ For␈α
this␈α
we␈α
want␈α
programs␈α
for␈α
getting␈α∞the␈α
maximum␈α
or␈α
the␈α
minimum␈α
of␈α
a␈α
function␈α∞on␈α
a
␈↓ ↓H␈↓list, and they are defined as follows:
␈↓ ↓H␈↓6.1) ␈↓ β7␈↓↓maxlis[u, f] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ -∞ ␈↓αelse␈↓↓ max[f[␈↓αa|␈↓↓u], maxlis[␈↓αd|␈↓↓u, f]]␈↓,
␈↓ ↓H␈↓6.2) ␈↓ βF␈↓↓minlis[u, f] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ∞ ␈↓αelse␈↓↓ min[f[␈↓αa|␈↓↓u], minlis[␈↓αd|␈↓↓u, f]]␈↓.
␈↓ ↓H␈↓Here␈αthe␈αsymbols␈α-∞␈αand␈α∞␈αrepresent␈αnumbers␈αthat␈αare␈αsmaller␈αand␈αlarger␈αthan␈αany␈αactual␈αvalues
␈↓ ↓H␈↓that␈αwill␈αoccur␈αin␈αevaluating␈α␈↓↓f.␈↓␈α If␈αthese␈α
numbers␈αare␈αnot␈αavailable,␈αthen␈αit␈αis␈αnecessary␈α
to␈αprime
␈↓ ↓H␈↓the␈α∞program␈α∞with␈α
the␈α∞values␈α∞of␈α
␈↓↓f␈↓␈α∞applied␈α∞to␈α
the␈α∞first␈α∞element␈α
of␈α∞the␈α∞list,␈α
and␈α∞the␈α∞programs␈α
are
␈↓ ↓H␈↓then␈αmeaningless␈αfor␈α
null␈αlists.␈α Iterative␈α
versions␈αof␈αthese␈αprograms␈α
are␈αgenerally␈αbetter;␈α
we␈αgive
␈↓ ↓H␈↓only the iterative version of ␈↓↓maxlis,␈↓ namely
␈↓ ↓H␈↓␈↓ ¬}Chapter II␈↓ *37
␈↓ ↓H␈↓␈↓ β⊗␈↓↓ maxlis[u, f] ← maxlisa[u, -∞, f], ␈↓
␈↓ ↓H␈↓6.3)
␈↓ ↓H␈↓␈↓ βλ␈↓↓maxlisa[u, x, f] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ maxlisa[␈↓αd|␈↓↓u, max[x, f[␈↓αa|␈↓↓u]], f]␈↓.
␈↓ ↓H␈↓ We now have
␈↓ ↓H␈↓6.4) ␈↓ β.␈↓↓vmax p ← ␈↓αif␈↓↓ ter p ␈↓αthen␈↓↓ imval p ␈↓αelse␈↓↓ maxlis[successors p, vmin]␈↓,
␈↓ ↓H␈↓6.5) ␈↓ β0␈↓↓vmin p ← ␈↓αif␈↓↓ ter p ␈↓αthen␈↓↓ imval p ␈↓αelse␈↓↓ minlis[successors p, vmax]␈↓.
␈↓ ↓H␈↓ The␈α
best␈α
move␈α
is␈αdetermined␈α
by␈α
the␈α
pair␈α
of␈αprograms␈α
(␈↓↓movemax,␈↓␈α
␈↓↓movemin).␈↓␈α
The␈α
key␈αto␈α
the
␈↓ ↓H␈↓computation␈α
is␈α
the␈α
pair␈α
of␈α
programs␈α
(␈↓↓bestmaxa,␈↓␈α
␈↓↓bestmina)␈↓␈α
which␈α
determine␈α
the␈α
optimal␈αmove␈α
from
␈↓ ↓H␈↓a␈α∂list␈α∂of␈α∂moves.␈α∂ They␈α∂call␈α∞each␈α∂other␈α∂recursively␈α∂and␈α∂differ␈α∂only␈α∞in␈α∂that␈α∂one␈α∂is␈α∂for␈α∂when␈α∞the
␈↓ ↓H␈↓maximizing␈αplayer␈αis␈αto␈αmove␈αand␈αthe␈αother␈αis␈αfor␈αwhen␈αthe␈αminimizing␈αplayer␈αis␈αto␈α
move.␈α The
␈↓ ↓H␈↓argument␈α
␈↓↓sofar␈↓␈α
is␈α
the␈α
best␈α
move␈α
found␈α
so␈α
far␈α
and␈α
␈↓↓bestsofar␈↓␈α
is␈α
value␈α
of␈α
the␈α
resulting␈α∞position␈α
as
␈↓ ↓H␈↓computed by ␈↓↓f.␈↓ Here are the definitions.
␈↓ ↓H␈↓6.6) ␈↓ ∧?␈↓↓movemax p ← bestmax[succesors p, vmin]␈↓,
␈↓ ↓H␈↓6.7) ␈↓ ∧5␈↓↓movemin p ← bestmin[succesors p, valmax]␈↓,
␈↓ ↓H␈↓where
␈↓ ↓H␈↓ ␈↓↓bestmax[u, f] ← bestmaxa[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u, f[␈↓αa|␈↓↓u], f]␈↓,
␈↓ ↓H␈↓6.8)
␈↓ ↓H␈↓ ␈↓↓bestmaxa[u, sofar, bestsofar, f] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ sofar␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ f[␈↓αa|␈↓↓u] ≤ bestsofar ␈↓αthen␈↓↓ bestmaxa[␈↓αd|␈↓↓u, sofar, bestsofar, f]␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ bestmaxa[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u, f[␈↓αa|␈↓↓u], f]␈↓,
␈↓ ↓H␈↓and
␈↓ ↓H␈↓ ␈↓↓bestmin[u, f] ← bestmina[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u, f[␈↓αa|␈↓↓u], f]␈↓,
␈↓ ↓H␈↓6.9)
␈↓ ↓H␈↓ ␈↓↓bestmina[u, sofar, bestsofar, f] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ sofar␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ f[␈↓αa|␈↓↓u] ≥ bestsofar ␈↓αthen␈↓↓ bestmina[␈↓αd|␈↓↓u, sofar, bestsofar, f]␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ bestmina[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u, f[␈↓αa|␈↓↓u], f]␈↓.
␈↓ ↓H␈↓ This␈α⊃straight␈α⊃minimax␈α⊃computation␈α⊃of␈α∩the␈α⊃best␈α⊃move␈α⊃does␈α⊃much␈α⊃more␈α∩computation␈α⊃in
␈↓ ↓H␈↓general than is usually necessary. The simplest way to see this is from the move tree of figure 5.
␈↓ ↓H␈↓38␈↓ ¬}Chapter II␈↓ H
␈↓"␈↓ ↓H␈↓␈↓ εX8
␈↓"␈↓ ↓H␈↓␈↓ ¬xmax␈↓ ε(≤'
␈↓"␈↓ ↓H␈↓␈↓ ελ≤'␈↓ ε_≥␈↓ ε_ααα 1
␈↓"␈↓ ↓H␈↓␈↓ ¬8min␈↓ ¬h≤'␈↓ ε(`≥
␈↓"␈↓ ↓H␈↓␈↓ ¬H≤'␈↓ εX6
␈↓"␈↓ ↓H␈↓␈↓ ¬8'␈↓ ¬8≥
␈↓"␈↓ ↓H␈↓␈↓ ¬H`≥␈↓ εX9
␈↓"␈↓ ↓H␈↓␈↓ ¬h`≥␈↓ ε(≤'
␈↓"␈↓ ↓H␈↓␈↓ ελ`≥␈↓ ε_'␈↓ ε_ααα x
␈↓"␈↓ ↓H␈↓␈↓ ε(`≥
␈↓"␈↓ ↓H␈↓␈↓ εXx
␈↓"␈↓ ↓H␈↓␈↓ ¬HFigure 5.
␈↓ ↓H␈↓We␈α∞see␈α∞from␈α∂this␈α∞figure␈α∞that␈α∞it␈α∂is␈α∞unnecessary␈α∞to␈α∞examine␈α∂the␈α∞moves␈α∞marked␈α∞ ␈↓x␈↓ ␈α∂because␈α∞their
␈↓ ↓H␈↓values␈αhave␈αno␈αeffect␈αon␈α
the␈αvalue␈αof␈αthe␈αgame␈α
or␈αon␈αthe␈αcorrect␈αmove␈α
since␈αthe␈αpresence␈αof␈αthe␈α
9
␈↓ ↓H␈↓is␈α∞sufficient␈α∞information␈α
at␈α∞this␈α∞point␈α
to␈α∞show␈α∞that␈α
the␈α∞move␈α∞leading␈α
to␈α∞the␈α∞vertex␈α∞preceding␈α
it
␈↓ ↓H␈↓should not be chosen by the minimizing player.
␈↓ ↓H␈↓ The␈α
general␈α
situation␈α
is␈α
that␈α
it␈α
is␈αunnecessary␈α
to␈α
examine␈α
further␈α
moves␈α
in␈α
a␈α
position␈αonce␈α
a
␈↓ ↓H␈↓move␈αis␈αfound␈α
that␈αrefutes␈αmoving␈αto␈α
the␈αposition␈αin␈α
the␈αfirst␈αplace.␈α This␈α
idea␈αis␈αcalled␈α
the␈ααβ-
␈↓ ↓H␈↓heuristic.␈α∪ As␈α∪an␈α∩example␈α∪of␈α∪how␈α∪this␈α∩heuristic␈α∪might␈α∪be␈α∪used,␈α∩we␈α∪describe␈α∪three␈α∪pairs␈α∩of
␈↓ ↓H␈↓programs:␈α#␈↓↓(valmax, valmin)␈↓,␈α#␈↓↓(linemax, linemin)␈↓␈α#and␈α#␈↓↓(treemax, treemin)␈↓.␈α# The␈α#programs
␈↓ ↓H␈↓␈↓↓(valmax, valmin)␈↓␈α∂return␈α∂the␈α∂value␈α∂of␈α∂the␈α∂game␈α∂to␈α∂the␈α∂corresponding␈α⊂player,␈α∂␈↓↓(linemax, linemin)␈↓
␈↓ ↓H␈↓return␈αthe␈αvalue␈αand␈αa␈αline␈αof␈αplay␈αobtaining␈αthat␈αvalue,␈αand␈α␈↓↓(treemax, treemin)␈↓␈αreturn␈αthe␈αvalue
␈↓ ↓H␈↓and␈αa␈α"proof"␈αthat␈αthis␈αis␈αthe␈αoptimal␈αvalue␈αfor␈αthe␈αcorresponding␈αplayer.␈α The␈α"proof"␈αconsists␈αof
␈↓ ↓H␈↓two␈αtrees␈αof␈αmoves␈αand␈αterminal␈αvalues␈αgiving␈αa␈αstrategy␈αguaranteeing␈αin␈αone␈αcase␈αthat␈αthe␈αvalue
␈↓ ↓H␈↓of␈α⊂the␈α∂game␈α⊂is␈α⊂at␈α∂least␈α⊂the␈α∂value␈α⊂returned␈α⊂and␈α∂in␈α⊂the␈α∂other␈α⊂case␈α⊂that␈α∂it␈α⊂is␈α∂at␈α⊂most␈α⊂the␈α∂value
␈↓ ↓H␈↓returned.
␈↓ ↓H␈↓ The␈α⊃use␈α⊂of␈α⊃the␈α⊂αβ-heuristic␈α⊃is␈α⊂the␈α⊃same␈α⊃for␈α⊂each␈α⊃pair␈α⊂of␈α⊃programs,␈α⊂they␈α⊃differ␈α⊃in␈α⊂the
␈↓ ↓H␈↓information␈α∂they␈α∂are␈α∂carrying␈α∂around␈α∂as␈α∂the␈α∂search␈α∂progresses.␈α∂ As␈α∂before␈α∂we␈α∂assume␈α∂that␈α∞the
␈↓ ↓H␈↓functions␈α⊗␈↓↓successors,␈↓␈α∃␈↓↓ter,␈↓␈α⊗and␈α⊗␈↓↓imval␈↓␈α∃characterize␈α⊗the␈α⊗game.␈α∃ For␈α⊗purposes␈α⊗of␈α∃determining
␈↓ ↓H␈↓properties␈α∂of␈α∂␈↓↓valmax␈↓␈α∂and␈α∂friends,␈α∂the␈α⊂program␈α∂␈↓↓rectify␈↓␈α∂can␈α∂be␈α∂assumed␈α∂to␈α∂compute␈α⊂the␈α∂identity
␈↓ ↓H␈↓function.␈α It␈αis␈αincluded␈αin␈αorder␈αto␈αallow␈αa␈αmore␈αefficient␈αrepresentation␈αof␈αthe␈αgame␈αand␈αwill␈αbe
␈↓ ↓H␈↓discussed␈α∂in␈α∂the␈α∂next␈α∂section.␈α∂ ␈↓↓ext␈↓␈α∂is␈α∂used␈α∂to␈α∂extract␈α∂from␈α∂a␈α∂position␈α∂the␈α∂move␈α∂leading␈α∂to␈α∂that
␈↓ ↓H␈↓position.
␈↓ ↓H␈↓ The␈α
programs␈α
(␈↓↓valmax,␈↓␈α
␈↓↓valmin)␈↓␈α∞each␈α
take␈α
three␈α
arguments:␈α
a␈α∞list␈α
of␈α
positions␈α
␈↓↓u,␈↓␈α∞and␈α
the
␈↓ ↓H␈↓parameters␈α␈↓↓alpha␈↓␈αand␈α␈↓↓beta.␈↓␈α ␈↓↓alpha␈↓␈αis␈αthe␈α
maximum␈αvalue␈αthat␈αcan␈αbe␈αguaranteed␈αthe␈α
maximizing
␈↓ ↓H␈↓player␈α∩based␈α∪on␈α∩the␈α∪search␈α∩so␈α∩far,␈α∪and␈α∩␈↓↓beta␈↓␈α∪is␈α∩the␈α∩least␈α∪value␈α∩that␈α∪can␈α∩be␈α∪guaranteed␈α∩the
␈↓ ↓H␈↓minimizing␈α⊂player␈α⊂based␈α⊂on␈α⊂the␈α⊂search␈α⊂so␈α⊂far.␈α⊂ Initially␈α⊂␈↓↓alpha␈↓␈α⊂is␈α⊂essentially␈α⊂-∞␈α⊂and␈α⊂␈↓↓beta␈↓␈α⊂is␈α⊂∞.
␈↓ ↓H␈↓␈↓↓valmax␈↓␈α∞works␈α∞as␈α∞follows.␈α∞ First␈α∞it␈α∞determines␈α∞the␈α∞value␈α∞of␈α∞the␈α∞next␈α∞position␈α∞on␈α∞the␈α∞list.␈α∂This␈α∞is
␈↓ ↓H␈↓done␈αby␈αcomputing␈α␈↓↓imval␈↓␈αif␈αit␈αis␈αterminal␈αor␈αapplying␈α␈↓↓valmin␈↓␈αto␈αits␈αsuccessors␈αif␈αnot.␈α If␈αthe␈αresult
␈↓ ↓H␈↓␈↓ ¬}Chapter II␈↓ *39
␈↓ ↓H␈↓is␈αnot␈αgreater␈αthan␈α␈↓↓alpha␈↓␈αthen␈αthis␈αmove␈αcan␈αbe␈αskipped␈αas␈αwe␈αare␈αguaranteed␈αto␈αbe␈αable␈αto␈αdo␈α
as
␈↓ ↓H␈↓well␈α
or␈α
better␈α
by␈α
a␈α
previously␈αfound␈α
move.␈α
If␈α
the␈α
value␈α
is␈α
not␈αless␈α
than␈α
␈↓↓beta␈↓␈α
then␈α
this␈α
whole␈αlist␈α
of
␈↓ ↓H␈↓moves␈α⊃can␈α⊃be␈α⊃ignored␈α⊃as␈α⊃␈↓↓beta␈↓␈α⊃is␈α⊃the␈α⊂best␈α⊃that␈α⊃the␈α⊃maximizing␈α⊃player␈α⊃can␈α⊃hope␈α⊃for␈α⊃and␈α⊂the
␈↓ ↓H␈↓minimizing␈αplayer␈αwould␈αnot␈αallow␈αthis␈αline␈αof␈αplay␈αif␈αit␈αproduced␈αa␈αhigher␈αvalue.␈α If␈αthe␈αvalue␈αis
␈↓ ↓H␈↓greater␈α
that␈α
␈↓↓alpha␈↓␈α
but␈α
less␈α∞than␈α
␈↓↓beta␈↓␈α
then␈α
this␈α
is␈α
a␈α∞better␈α
choice␈α
for␈α
the␈α
maximizing␈α∞player␈α
and
␈↓ ↓H␈↓␈↓↓alpha␈↓␈α
is␈α
replaced␈αby␈α
the␈α
new␈αvalue␈α
and␈α
the␈αsearch␈α
continues.␈α
When␈αthe␈α
list␈α
is␈α
exhausted␈α␈↓↓alpha␈↓
␈↓ ↓H␈↓represents␈α
the␈α∞maximum␈α
value␈α
that␈α∞the␈α
maximizing␈α∞player␈α
can␈α
be␈α∞guaranteed␈α
starting␈α∞from␈α
the
␈↓ ↓H␈↓position␈α∪that␈α∩gave␈α∪rise␈α∪to␈α∩the␈α∪initial␈α∪list␈α∩of␈α∪moves.␈α∩ ␈↓↓valmin␈↓␈α∪works␈α∪in␈α∩a␈α∪dual␈α∪fashion.␈α∩ The
␈↓ ↓H␈↓definitions of ␈↓↓(valmax valmin)␈↓ are:
␈↓ ↓H␈↓␈↓ α8␈↓↓ valmax[u, alpha, beta] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ alpha␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ {␈↓αif␈↓↓ ter[rectify ␈↓αa|␈↓↓u, alpha, beta] ␈↓αthen␈↓↓ imval ␈↓αa|␈↓↓u␈↓
␈↓ ↓H␈↓6.10)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ valmin[successors ␈↓αa|␈↓↓u, alpha, beta]}␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [λs: ␈↓αif␈↓↓ ¬[s > alpha] ␈↓αthen␈↓↓ valmax[␈↓αd|␈↓↓u, alpha, beta]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ s < beta ␈↓αthen␈↓↓ valmax[␈↓αd|␈↓↓u, s, beta]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ beta]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ valmin[u, alpha, beta] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ beta␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ {␈↓αif␈↓↓ ter[rectify ␈↓αa|␈↓↓u, alpha, beta] ␈↓αthen␈↓↓ imval ␈↓αa|␈↓↓u␈↓
␈↓ ↓H␈↓6.11)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ valmax[successors ␈↓αa|␈↓↓u, alpha, beta]}␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [λs: ␈↓αif␈↓↓ ¬[s > alpha] ␈↓αthen␈↓↓ alpha␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ s < beta ␈↓αthen␈↓↓ valmin[␈↓αd|␈↓↓u, alpha, s]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ valmin[␈↓αd|␈↓↓u, alpha, beta]]␈↓
␈↓ ↓H␈↓ The␈α⊃programs␈α⊃(␈↓↓linemax,␈↓␈α⊃␈↓↓linemin)␈↓␈α⊃each␈α⊃take␈α⊂four␈α⊃arguments,␈α⊃three␈α⊃are␈α⊃the␈α⊃same␈α⊃as␈α⊂for
␈↓ ↓H␈↓(␈↓↓valmax,␈↓␈α∂␈↓↓valmin)␈↓␈α∂and␈α⊂the␈α∂fourth,␈α∂␈↓↓line␈↓␈α⊂gives␈α∂the␈α∂first␈α⊂line␈α∂of␈α∂play␈α⊂(list␈α∂of␈α∂moves)␈α⊂found␈α∂which
␈↓ ↓H␈↓terminates␈α
in␈α
a␈α
position␈α
of␈αvalue␈α
␈↓↓alpha␈↓␈α
for␈α
␈↓↓linemax␈↓␈α
or␈α
␈↓↓beta␈↓␈αfor␈α
␈↓↓linemin.␈↓␈α
The␈α
result␈α
returned␈αis␈α
the
␈↓ ↓H␈↓optimal␈α
value␈α
␈↓↓cons␈↓ed␈α
onto␈α
the␈α
list␈α
of␈α
moves␈αgiving␈α
the␈α
first␈α
line␈α
of␈α
play␈α
found␈α
attaining␈αthat␈α
value.
␈↓ ↓H␈↓In␈α∂the␈α∞case␈α∂of␈α∂␈↓↓linemax␈↓␈α∞if␈α∂a␈α∂better␈α∞value␈α∂of␈α∂␈↓↓alpha␈↓␈α∞has␈α∂not␈α∂been␈α∞found␈α∂then␈α∂the␈α∞line␈α∂of␈α∂play␈α∞is
␈↓ ↓H␈↓irrelevant␈α∞as␈α∞the␈α∞calling␈α∞function␈α∞␈↓↓linemin␈↓␈α∞will␈α∞ignore␈α∞it.␈α∞ At␈α∞the␈α∞top␈α∞level␈α∞if␈α∞␈↓↓alpha␈↓␈α∞and␈α∞␈↓↓beta␈↓␈α∞are
␈↓ ↓H␈↓initialized␈αso␈α
as␈αto␈α
insure␈αthat␈α
a␈αbetter␈α
value␈αwill␈α
be␈αfound␈α
the␈αinitial␈α
value␈αof␈α
␈↓↓line␈↓␈αis␈α
unimportant.
␈↓ ↓H␈↓The definitions of ␈↓↓(linemax linemin)␈↓ are:
␈↓ ↓H␈↓␈↓ α8␈↓↓ linemax[u, line, alpha, beta] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ alpha . line␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ {␈↓αif␈↓↓ ter[rectify ␈↓αa|␈↓↓u, alpha, beta] ␈↓αthen␈↓↓ <imval ␈↓αa|␈↓↓u>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ linemin[␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ successors ␈↓αa|␈↓↓u, ␈↓
␈↓ ↓H␈↓6.12)␈↓ α8␈↓↓ beta . ␈↓¬BETA-CUTOFF␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ alpha, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ beta]}␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [λs: ␈↓αif␈↓↓ ¬[␈↓αa|␈↓↓s > alpha] ␈↓αthen␈↓↓ linemax[␈↓αd|␈↓↓u, line, alpha, beta]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓s < beta ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ linemax[␈↓αd|␈↓↓u, ext ␈↓αa|␈↓↓u . ␈↓αd|␈↓↓s, ␈↓αa|␈↓↓s, beta]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ beta . line]␈↓
␈↓ ↓H␈↓40␈↓ ¬}Chapter II␈↓ H
␈↓ ↓H␈↓␈↓ α8␈↓↓ linemin[u, line, alpha, beta] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ beta . line␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ {␈↓αif␈↓↓ ter[rectify ␈↓αa|␈↓↓u, alpha, beta] ␈↓αthen␈↓↓ <imval ␈↓αa|␈↓↓u>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ linemax[␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ successors ␈↓αa|␈↓↓u, ␈↓
␈↓ ↓H␈↓6.13)␈↓ α8␈↓↓ alpha . ␈↓¬ALPHA-CUTOFF␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ alpha, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ beta]}␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [λs: ␈↓αif␈↓↓ ¬[␈↓αa|␈↓↓s > alpha] ␈↓αthen␈↓↓ alpha . line␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓s < beta ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ linemin[␈↓αd|␈↓↓u, ext ␈↓αa|␈↓↓u . ␈↓αd|␈↓↓s, alpha, ␈↓αa|␈↓↓s]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ linemin[␈↓αd|␈↓↓u, line, alpha, beta]]␈↓
␈↓ ↓H␈↓ The␈α
programs␈α
(␈↓↓treemax,␈↓␈α␈↓↓treemin)␈↓␈α
each␈α
take␈αfive␈α
arguments.␈α
In␈αaddition␈α
to␈α
the␈α
three␈αused
␈↓ ↓H␈↓by␈α
(␈↓↓valmax,␈↓␈α
␈↓↓valmin)␈↓␈αare␈α
␈↓↓trmax␈↓␈α
and␈α␈↓↓trmin.␈↓␈α
These␈α
are␈α
move␈αtrees␈α
including␈α
terminal␈αvalues,␈α
which
␈↓ ↓H␈↓are␈α∃used␈α∃to␈α∀build␈α∃the␈α∃proofs␈α∃previously␈α∀mentioned.␈α∃ If␈α∃a␈α∀position␈α∃␈↓↓p␈↓␈α∃is␈α∃non-terminal␈α∀and
␈↓ ↓H␈↓␈↓↓u = successors p␈↓␈αthen␈α
␈↓↓treemax[u, ␈↓¬NIL␈↓↓, ␈↓¬NIL␈↓↓, α, β]␈↓␈αreturns␈α
a␈αlist␈α
␈↓↓<val, pfmax, pfmin>␈↓␈αwhere␈αone␈α
of
␈↓ ↓H␈↓the following is true:
␈↓ ↓H␈↓␈↓ αλ1) ␈↓↓val␈↓ ≤ α and ␈↓↓pfmin␈↓ is a proof that the value of ␈↓↓p␈↓ to the maximizing player is at most ␈↓↓val.␈↓
␈↓ ↓H␈↓␈↓ αλ2)␈αα␈α<␈α␈↓↓val␈↓␈α<␈αβ,␈α␈↓↓pfmax␈↓␈αis␈αa␈αproof␈αthat␈αthe␈αvalue␈αof␈α␈↓↓p␈↓␈αto␈αthe␈αmaximizing␈αplayer␈αis␈αat␈αleast␈α␈↓↓val␈↓
␈↓ ↓H␈↓␈↓ αHand ␈↓↓pfmin␈↓ is a proof that it is at most ␈↓↓val.␈↓
␈↓ ↓H␈↓␈↓ αλ3) β ≤ ␈↓↓val␈↓ and ␈↓↓pfmax␈↓ is a proof that the value of ␈↓↓p␈↓ to the maximizing player is at least ␈↓↓val.␈↓
␈↓ ↓H␈↓Similarly␈α
␈↓↓treemin[u, ␈↓¬NIL␈↓↓, ␈↓¬NIL␈↓↓, α, β]␈↓␈α
returns␈α
a␈α∞list␈α
␈↓↓<val, pfmax, pfmin>␈↓␈α
where␈α
the␈α
value␈α∞is␈α
now
␈↓ ↓H␈↓relative to the minimizing player.
␈↓ ↓H␈↓ Note␈α
that␈αin␈α
the␈α
case␈α␈↓↓val ≤ α (≥ β)␈↓␈α
we␈αmake␈α
no␈α
requirement␈αof␈α
␈↓↓pfmax␈↓␈α(␈↓↓pfmin).␈↓␈α
This␈α
is␈αin
␈↓ ↓H␈↓these␈α
cases␈α
the␈α
searches␈αwere␈α
probably␈α
incomplete␈α
and␈α
the␈αresulting␈α
proof␈α
would␈α
make␈α
no␈αsense.
␈↓ ↓H␈↓The definitions of ␈↓↓(treemax, treemin)␈↓ are:
␈↓ ↓H␈↓␈↓ ¬}Chapter II␈↓ *41
␈↓ ↓H␈↓␈↓ α8␈↓↓ treemax[u, trmax, trmin, alpha, beta] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ <alpha, trmax, trmin>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ {␈↓αif␈↓↓ ter[rectify ␈↓αa|␈↓↓u, alpha, beta] ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ {imval ␈↓αa|␈↓↓u}[λv: <v, <v>, <v>>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ treemin[␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ successors ␈↓αa|␈↓↓u, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬NIL␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ beta . ␈↓¬BETA-CUTOFF␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ alpha, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ beta]}␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [λs: ␈↓αif␈↓↓ ¬[␈↓αa|␈↓↓s > alpha] ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓6.14)␈↓ α8␈↓↓ treemax[␈↓αd|␈↓↓u, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ trmax, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [ext ␈↓αa|␈↓↓u . ␈↓αadd|␈↓↓s] . trmin, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ alpha, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ beta]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓s < beta ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ treemax[␈↓αd|␈↓↓u, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ext ␈↓αa|␈↓↓u . ␈↓αad|␈↓↓s, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [ext ␈↓αa|␈↓↓u . ␈↓αadd|␈↓↓s] . trmin, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αa|␈↓↓s, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ beta]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ <beta, ext ␈↓αa|␈↓↓u . ␈↓αad|␈↓↓s, ␈↓¬NIL␈↓↓>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ treemin[u, trmax, trmin, alpha, beta] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ <beta, trmax, trmin>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ {␈↓αif␈↓↓ ter[rectify ␈↓αa|␈↓↓u, alpha, beta] ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ {imval ␈↓αa|␈↓↓u}[λv: <v, <v>, <v>>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ treemax[␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ successors ␈↓αa|␈↓↓u, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ alpha . ␈↓¬ALPHA-CUTOFF␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬NIL␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ alpha, ␈↓
␈↓ ↓H␈↓6.15)␈↓ α8␈↓↓ beta]}␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [λs: ␈↓αif␈↓↓ ¬[␈↓αa|␈↓↓s > alpha] ␈↓αthen␈↓↓ <alpha, ␈↓¬NIL␈↓↓, ext ␈↓αa|␈↓↓u . ␈↓αadd|␈↓↓s>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓s < beta ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ treemin[␈↓αd|␈↓↓u, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [ext ␈↓αa|␈↓↓u . ␈↓αad|␈↓↓s] . trmax, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ext ␈↓αa|␈↓↓u . ␈↓αadd|␈↓↓s, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ alpha, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αa|␈↓↓s]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ treemin[␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αd|␈↓↓u, [ext ␈↓αa|␈↓↓u . ␈↓αad|␈↓↓s] . trmax, trmin, alpha, beta]]␈↓
␈↓ ↓H␈↓ We␈α⊂now␈α⊂describe␈α⊂how␈α⊂a␈α⊂move␈α⊂tree,␈α⊃␈↓↓pf,␈↓␈α⊂represents␈α⊂a␈α⊂proof␈α⊂of␈α⊂the␈α⊂sort␈α⊃required␈α⊂above.
␈↓ ↓H␈↓There are four cases.
␈↓ ↓H␈↓1)␈α␈↓↓pf␈↓␈α
is␈αa␈α
proof␈αthat␈αthe␈α
value␈αof␈α
␈↓↓p␈↓␈αto␈αthe␈α
maximizing␈αplayer␈α
is␈αat␈αleast␈α
␈↓↓val␈↓␈αif␈α
one␈αof␈αthe␈α
following
␈↓ ↓H␈↓holds:
␈↓ ↓H␈↓42␈↓ ¬}Chapter II␈↓ H
␈↓ ↓H␈↓ (i) ␈↓↓p␈↓ is terminal and ␈↓↓pf = <imval p>␈↓ and ␈↓↓imval p ≥ val␈↓
␈↓ ↓H␈↓ (ii)␈α
␈↓↓p␈↓␈α
is␈αnot␈α
terminal␈α
and␈α
␈↓↓pf = mv . pf'␈↓␈αwhere␈α
␈↓↓mv␈↓␈α
is␈α
a␈αmove␈α
leading␈α
to␈α␈↓↓p' ε successors p␈↓␈α
and
␈↓ ↓H␈↓␈↓↓pf'␈↓ is a proof that the value of ␈↓↓p'␈↓ to the minimizing player is at least ␈↓↓val.␈↓
␈↓ ↓H␈↓2)␈α␈↓↓pf␈↓␈αis␈αa␈αproof␈αthat␈αthe␈α
value␈αof␈α␈↓↓p␈↓␈αto␈αthe␈αminimizing␈αplayer␈α
is␈αat␈αleast␈α␈↓↓val␈↓␈αif␈αone␈αof␈αthe␈α
following
␈↓ ↓H␈↓holds:
␈↓ ↓H␈↓ (i) ␈↓↓p␈↓ is terminal and ␈↓↓pf = <imval p>␈↓ and ␈↓↓imval p ≥ val␈↓.
␈↓ ↓H␈↓ (ii)␈α⊃␈↓↓p␈↓␈α⊃is␈α⊃not␈α⊃terminal␈α⊃and␈α⊃␈↓↓pf␈↓␈α⊃is␈α⊃a␈α⊃list␈α⊃of␈α⊃elements␈α⊃of␈α⊃the␈α⊃form␈α⊃␈↓↓mv . pf'␈↓,␈α⊃one␈α⊃for␈α⊃each
␈↓ ↓H␈↓␈↓↓p' ε successors p,␈↓␈αsuch␈αthat␈α␈↓↓mv␈↓␈αis␈αa␈αmove␈αleading␈αfrom␈α␈↓↓p␈↓␈αto␈α␈↓↓p'␈↓␈αand␈α␈↓↓pf'␈↓␈αis␈αa␈αproof␈αthat␈αthe␈αvalue
␈↓ ↓H␈↓of ␈↓↓p'␈↓ to the maximizing player is at least ␈↓↓val.␈↓
␈↓ ↓H␈↓Cases␈α∩3)␈α∩and␈α∪4)␈α∩are␈α∩obtained␈α∩from␈α∪1)␈α∩and␈α∩2)␈α∩by␈α∪interchanging␈α∩the␈α∩words␈α∪maximizing␈α∩and
␈↓ ↓H␈↓minimizing and reversing the sense of the inequalities throughout.
␈↓ ↓H␈↓ The␈α
αβ␈α
algorithm␈α
can␈α
also␈α
be␈αused␈α
to␈α
in␈α
the␈α
case␈α
that␈α
there␈αis␈α
a␈α
way␈α
to␈α
estimate␈α
the␈αvalue␈α
of
␈↓ ↓H␈↓a␈α⊃position␈α⊃␈↓↓p.␈↓␈α⊃ Suppose␈α⊃we␈α⊃estimate␈α⊃that␈α⊃␈↓↓a < vmax p < b␈↓.␈α⊃ Then␈α⊃if␈α⊃␈↓↓valmax[p, a, b] = val␈↓␈α⊃with
␈↓ ↓H␈↓␈↓↓a < val < b␈↓␈α⊂then␈α⊂we␈α∂win␈α⊂as␈α⊂we␈α∂now␈α⊂have␈α⊂the␈α∂correct␈α⊂value␈α⊂and␈α∂hopefully␈α⊂the␈α⊂narrower␈α∂range
␈↓ ↓H␈↓caused␈α
even␈α
more␈α
pruning␈αthan␈α
usual.␈α
If␈α
␈↓↓val ≤a␈↓␈αthen␈α
we␈α
must␈α
lower␈αthe␈α
estimate␈α
of␈α
the␈αvalue␈α
and
␈↓ ↓H␈↓try again. Similarly if ␈↓↓val ≥ b␈↓ then we raise the extimate.
␈↓ ↓H␈↓ The␈αreduction␈αin␈αnumber␈αof␈αpositions␈αexamined␈αgiven␈αby␈αthe␈ααβ␈αalgorithm␈αover␈αthe␈αsimple
␈↓ ↓H␈↓minimax␈α
algorithm␈α
depends␈α
on␈α
the␈α
order␈α
in␈α
which␈α
the␈α
moves␈α
are␈α
examined.␈α
In␈α
the␈α∞worst␈α
case,
␈↓ ↓H␈↓the␈αmoves␈αhappen␈αto␈αbe␈αexamined␈αin␈αinverse␈αorder␈αof␈αmerit␈αin␈αevery␈αposition␈αon␈αthe␈αtree,␈αi.e.␈αthe
␈↓ ↓H␈↓worst␈αmove␈αfirst.␈α In␈αthat␈αcase,␈αthere␈αis␈αno␈αimprovement␈αover␈αminimax.␈α The␈αbest␈αcase␈αis␈αthe␈αone
␈↓ ↓H␈↓in␈α
which␈α
the␈α
best␈α
move␈α
in␈α
every␈α
position␈α
is␈α
examined␈α
first.␈α
If␈α
we␈α
look␈α
␈↓↓n␈↓␈α
moves␈α
deep␈α
on␈α
a␈αtree
␈↓ ↓H␈↓that␈αhas␈α␈↓↓k␈↓␈αsuccessors␈αto␈αeach␈αposition,␈αthen␈αminimax␈αlooks␈αat␈α␈↓↓k␈↓∧n␈↓␈αpositions␈αwhile␈ααβ␈αlooks␈αat␈αabout
␈↓ ↓H␈↓only␈α∂␈↓↓k␈↓∧n/2␈↓.␈α∂ Thus␈α∂a␈α⊂program␈α∂that␈α∂looks␈α∂at␈α⊂10␈↓∧4␈↓␈α∂moves␈α∂with␈α∂αβ␈α∂might␈α⊂have␈α∂to␈α∂look␈α∂at␈α⊂10␈↓∧8␈↓␈α∂with
␈↓ ↓H␈↓minimax.␈α For␈αthis␈αreason,␈αgame␈αplaying␈αprograms␈αusing␈ααβ␈αmake␈αa␈αbig␈αeffort␈αto␈αinclude␈αas␈αgood
␈↓ ↓H␈↓as␈α
possible␈αan␈α
ordering␈αof␈α
moves␈αinto␈α
the␈α␈↓↓successors␈↓␈α
function.␈α When␈α
there␈αis␈α
a␈αdeep␈α
tree␈αsearch␈α
to
␈↓ ↓H␈↓be␈αdone,␈αthe␈αway␈αto␈αmake␈αthe␈αordering␈αis␈αwith␈αa␈αshort␈αlook-ahead␈αto␈αa␈αmuch␈αsmaller␈αdepth␈αthan
␈↓ ↓H␈↓the␈α⊂main␈α∂search.␈α⊂ Still␈α⊂shorter␈α∂look-aheads␈α⊂are␈α∂used␈α⊂deeper␈α⊂in␈α∂the␈α⊂tree,␈α∂and␈α⊂beyond␈α⊂a␈α∂certain
␈↓ ↓H␈↓depth, non-look-ahead ordering methods are of decreasing complexity.
␈↓ ↓H␈↓ A␈α
version␈α
of␈α
αβ␈α
incorporating␈α∞optimistic␈α
and␈α
pessimistic␈α
evaluations␈α
of␈α
positions␈α∞was␈α
first
␈↓ ↓H␈↓proposed␈α∂by␈α∂McCarthy␈α∂about␈α∂1958.␈α∂ Edwards␈α∂and␈α∂Hart␈α∂at␈α∂M.I.T.␈α∂about␈α∂1959␈α∂proved␈α∂that␈α∞the
␈↓ ↓H␈↓present␈α∞version␈α∞of␈α∞αβ␈α∂works␈α∞and␈α∞calculated␈α∞the␈α∞improvement␈α∂it␈α∞gives␈α∞over␈α∞minimax.␈α∂ The␈α∞first
␈↓ ↓H␈↓publication,␈α∃however,␈α⊗was␈α∃Brudno␈α∃[1963].␈α⊗ For␈α∃additional␈α∃discussion␈α⊗of␈α∃αβ␈α⊗techinques␈α∃see
␈↓ ↓H␈↓Nilsson[1971], Knuth and Moore[1975] or Winston[1977].
␈↓ ↓H␈↓ It␈αis␈αworth␈αnoting␈αthat␈ααβ␈αwas␈αnot␈αused␈αin␈αthe␈αearly␈αchess␈αplaying␈αprograms␈αin␈αspite␈αof␈αthe
␈↓ ↓H␈↓fact␈αthat␈αit␈αis␈αclearly␈αused␈αin␈αany␈αhuman␈αplay.␈α Its␈αnon-use,␈αtherefore,␈αrepresents␈αa␈αfailure␈αof␈αself-
␈↓ ↓H␈↓observation.␈α Very␈αlikely,␈αthere␈αare␈αa␈αnumber␈αof␈αother␈αalgorithms␈αused␈αin␈αhuman␈αthought␈αthat␈αwe
␈↓ ↓H␈↓have␈α⊃not␈α⊃noticed␈α⊂and␈α⊃incorporated␈α⊃in␈α⊂our␈α⊃programs.␈α⊃ To␈α⊂the␈α⊃extent␈α⊃that␈α⊂this␈α⊃is␈α⊃so,␈α⊂artificial
␈↓ ↓H␈↓intelligence will be ␈↓↓a posteriori␈↓ obvious even though it is ␈↓↓a priori␈↓ very difficult.
␈↓ ↓H␈↓␈↓ ¬}Chapter II␈↓ *43
␈↓ ↓H␈↓7. ␈↓αThe hidden board trick.␈↓
␈↓ ↓H␈↓ The␈α⊃program␈α⊃␈↓↓rectify␈↓␈α⊃is␈α⊂used␈α⊃to␈α⊃implement␈α⊃a␈α⊂particular␈α⊃method␈α⊃of␈α⊃representing␈α⊃a␈α⊂game
␈↓ ↓H␈↓known␈α⊂as␈α⊂the␈α⊂"hidden␈α⊂board␈α⊂trick".␈α⊂ It␈α∂basically␈α⊂relies␈α⊂on␈α⊂side␈α⊂effects␈α⊂to␈α⊂produce␈α⊂an␈α∂alternate
␈↓ ↓H␈↓representation␈α
of␈αa␈α
given␈α
position␈αwhen␈α
it␈α
is␈αneeded.␈α
Here␈α
a␈αposition␈α
is␈α
represented␈αby␈α
a␈α
list␈αof
␈↓ ↓H␈↓moves␈α
leading␈α
to␈α
it␈α
with␈α
the␈αmost␈α
recent␈α
move␈α
at␈α
the␈α
head␈α
of␈αthe␈α
list␈α
and␈α
the␈α
first␈α
move␈α
of␈αthe
␈↓ ↓H␈↓game␈α∞at␈α∞the␈α∂end␈α∞of␈α∞the␈α∂list.␈α∞ This␈α∞is␈α∞an␈α∂efficient␈α∞representation␈α∞with␈α∂respect␈α∞to␈α∞storage␈α∂for␈α∞two
␈↓ ↓H␈↓reasons.␈α One␈αis␈αthat␈αlists␈αof␈αpositions␈αwill␈αfrequently␈αhave␈αonly␈αthe␈αlast␈αfew␈αmoves␈αdiffering␈αand
␈↓ ↓H␈↓can␈αbe␈αrepresented␈αas␈αmerging␈αlist␈αstructures.␈α The␈αsecond␈αis␈αthat␈αother␈αrepresentations␈αfrequently
␈↓ ↓H␈↓take␈αthe␈αform␈αof␈αa␈αcollection␈αof␈αtables␈αand␈αthe␈αrepresentation␈αof␈αa␈αposition␈αrequires␈αa␈αlot␈αof␈αspace.
␈↓ ↓H␈↓However␈α⊂it␈α⊂is␈α⊂often␈α⊂easier␈α⊂to␈α⊂compute␈α⊂the␈α⊂successors,␈α⊂determine␈α⊂if␈α⊂a␈α⊂position␈α⊂is␈α⊂terminal,␈α⊂and
␈↓ ↓H␈↓compute␈αits␈αvalue␈α
using␈αa␈αmore␈αspacious␈α
representation.␈α To␈αtake␈αadvantage␈α
of␈αthis␈αfact␈α
we␈αkeep
␈↓ ↓H␈↓around␈α
a␈α
representation␈α
of␈α
the␈α
position␈α
of␈α
current␈α
interest,␈α
the␈α
hidden␈α
board,␈α
in␈α
a␈α
form␈α
convenient
␈↓ ↓H␈↓for␈αdoing␈αthese␈αcomputations.␈α A␈αlist␈αof␈αmoves␈αleading␈αto␈αthat␈αposition␈αis␈αalso␈αkept.␈α The␈αfunction
␈↓ ↓H␈↓␈↓↓rectify␈↓␈α
is␈αused␈α
to␈αmake␈α
a␈α
new␈αposition␈α
current.␈α It␈α
returns␈α
the␈αnew␈α
position␈αunchanged,␈α
but␈αhas␈α
the
␈↓ ↓H␈↓side␈α∞effect␈α∞of␈α∞making␈α∞it␈α∞the␈α∞current␈α∞position␈α∞as␈α∞far␈α∞as␈α∞the␈α∞alternate␈α∞representation␈α∞is␈α∞concerned.
␈↓ ↓H␈↓This␈α∂is␈α∂accomplished␈α∂by␈α∂using␈α⊂␈↓↓commontail␈↓␈α∂to␈α∂find␈α∂out␈α∂where␈α⊂in␈α∂the␈α∂past␈α∂the␈α∂current␈α⊂and␈α∂new
␈↓ ↓H␈↓positions␈αmeet␈α,␈αbacking␈αup␈αto␈αthis␈αposition␈αand␈αthen␈αmoving␈αforward␈αalong␈αthe␈αpath␈αof␈αthe␈αnew
␈↓ ↓H␈↓position.␈α∞ This␈α∞requires␈α∂the␈α∞additional␈α∞game␈α∞dependent␈α∂functions␈α∞␈↓↓revert␈↓␈α∞for␈α∞taking␈α∂back␈α∞moves,
␈↓ ↓H␈↓and ␈↓↓update␈↓ for making moves. The programs for ␈↓↓rectify␈↓ and its auxiliaries follow.
␈↓ ↓H␈↓␈↓ β8␈↓↓ rectify p ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓αprogram␈↓↓ [z, q]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ q ← commontail[p, p1]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓¬L1:␈↓↓ ␈↓αif␈↓↓ q = p1 ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L2␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ revert[]␈↓
␈↓ ↓H␈↓7.1)␈↓ β8␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L1␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓¬L2:␈↓↓ z ← listsubt[p, p1]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓¬L3:␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓z ␈↓αthen␈↓↓ return p␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ update ␈↓αa|␈↓↓z␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ z ← ␈↓αd|␈↓↓z␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L3␈↓↓␈↓
␈↓ ↓H␈↓7.2)␈↓ β8␈↓↓ commontail[u, v] ← reverse commonhead[reverse u, reverse v]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ commonhead[u, v] ← ␈↓
␈↓ ↓H␈↓7.3)␈↓ β8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ∨ ␈↓αn|␈↓↓v ∨ ¬[␈↓αa|␈↓↓u = ␈↓αa|␈↓↓v] ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . commonhead[␈↓αd|␈↓↓u, ␈↓αd|␈↓↓v]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ listsubt[u, v] ← listsubta[u, length u - length v, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓7.4)
␈↓ ↓H␈↓␈↓ β8␈↓↓ listsubta[u, n, z] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓αif␈↓↓ n = 0 ␈↓αthen␈↓↓ z ␈↓αelse␈↓↓ listsubta[␈↓αd|␈↓↓u, sub1 n, ␈↓αa|␈↓↓u . z]␈↓
␈↓ ↓H␈↓44␈↓ ¬}Chapter II␈↓ H
␈↓ ↓H␈↓8. ␈↓α2-dimensional Tic Tac Toe.␈↓
␈↓ ↓H␈↓ As␈α∞a␈α∞simple␈α
example␈α∞of␈α∞how␈α∞the␈α
programs␈α∞described␈α∞in␈α∞the␈α
previous␈α∞two␈α∞sections␈α∞can␈α
be
␈↓ ↓H␈↓used␈αwe␈αgive␈αa␈αset␈αof␈αprograms␈αcharacterizing␈αthe␈αgame␈αof␈α2-dimensional␈αTic␈αTac␈αToe␈αand␈αshow
␈↓ ↓H␈↓the␈α∞results␈α∞of␈α∞some␈α∂computations␈α∞using␈α∞each␈α∞of␈α∞the␈α∂three␈α∞pairs␈α∞of␈α∞programs␈α∞employing␈α∂the␈α∞αβ-
␈↓ ↓H␈↓heuristic.␈α This␈αcharacterization␈αof␈αTic␈αTac␈αToe␈αimproves␈αthe␈αefficiency␈αof␈αthe␈αsearch␈αroutines␈αby
␈↓ ↓H␈↓doing␈αsome␈αprecomputing␈αof␈αposition␈αvalues␈αand␈αthus␈αpruning␈αand␈αordering␈αthe␈αlist␈αof␈αsuccessors
␈↓ ↓H␈↓for a given position.
␈↓ ↓H␈↓ The␈αnine␈αsquares␈αof␈αthe␈αTic␈αTac␈αToe␈αboard␈αare␈αnumbered␈αfrom␈αleft␈αto␈αright␈αstarting␈αfrom
␈↓ ↓H␈↓the␈α
top␈α
row.␈α
There␈α
are␈α
several␈α
global␈α
arrays␈α
and␈α
variables.␈α
These␈α
are␈α
initialized␈α
by␈α
the␈α
programs
␈↓ ↓H␈↓␈↓↓commence␈↓␈α∂and␈α∂␈↓↓newgame.␈↓␈α⊂ The␈α∂ith␈α∂element␈α⊂of␈α∂␈↓↓lines␈↓␈α∂is␈α⊂a␈α∂list␈α∂of␈α⊂lines␈α∂containing␈α∂the␈α⊂ith␈α∂square.
␈↓ ↓H␈↓␈↓↓xcount␈↓␈α∞tells␈α∂how␈α∞many␈α∂␈↓↓X␈↓'s␈α∞are␈α∂on␈α∞a␈α∂given␈α∞line,␈α∞␈↓↓ocount␈↓␈α∂does␈α∞the␈α∂same␈α∞for␈α∂␈↓↓O␈↓'s.␈α∞ ␈↓↓p1␈↓␈α∂is␈α∞the␈α∂list␈α∞of
␈↓ ↓H␈↓moves␈α∩leading␈α∩to␈α⊃the␈α∩position␈α∩represented␈α∩by␈α⊃the␈α∩state␈α∩of␈α∩the␈α⊃global␈α∩variables.␈α∩ (A␈α∩move␈α⊃is
␈↓ ↓H␈↓represented␈α
by␈αthe␈α
number␈αof␈α
the␈α
square␈αfilled.) ␈α
␈↓↓xs,␈↓␈α␈↓↓os,␈↓␈α
and␈α
␈↓↓bs␈↓␈αare␈α
lists␈αof␈α
the␈αsquares␈α
containing
␈↓ ↓H␈↓␈↓↓X␈↓'s,␈α
␈↓↓O␈↓'s,␈α
and␈α
blanks␈α
respectively.␈α
␈↓↓w␈↓␈α
tells␈α
whose␈α
turn␈αit␈α
is␈α
to␈α
move.␈α
If␈α
␈↓¬T␈↓␈α
then␈α
it␈α
is␈α
the␈α␈↓↓X␈↓-players
␈↓ ↓H␈↓turn,␈αif␈α␈↓¬NIL␈↓␈αthen␈αit␈αis␈αthe␈α␈↓↓O␈↓-players␈αturn.␈α ␈↓↓level␈↓␈αis␈αthe␈αnumber␈αof␈αmoves␈αso␈αfar␈α(the␈αlength␈αof␈α␈↓↓p1).␈↓
␈↓ ↓H␈↓␈↓↓count␈↓ is the total number of moves examined.
␈↓ ↓H␈↓ For␈α∞the␈α∞␈↓↓X␈↓-player␈α∞a␈α∞winning␈α∞position␈α
has␈α∞value␈α∞10-␈↓↓level,␈↓␈α∞any␈α∞other␈α∞terminal␈α∞position␈α
has
␈↓ ↓H␈↓value␈α0.␈α For␈αthe␈α␈↓↓O␈↓-player␈αa␈αwinning␈αposition␈αhas␈αvalue␈α␈↓↓level-10␈↓␈αand␈αany␈αother␈αterminal␈αposition
␈↓ ↓H␈↓has value 0.
␈↓ ↓H␈↓ A␈αposition␈αis␈αconsidered␈αterminal␈αif␈αthere␈αare␈αno␈αblank␈αspaces␈αleft,␈αor␈αif␈α␈↓↓10 - level ≤ α␈↓,␈αor␈αif
␈↓ ↓H␈↓␈↓↓-10 + level ≥β␈↓,␈αor␈αif␈αit␈αis␈αthe␈α␈↓↓X␈↓-players␈αturn␈αto␈αmove␈αand␈α␈↓↓xcount␈↓␈αis␈α3␈αfor␈αsome␈αline,␈αor␈αif␈αit␈αis␈αthe
␈↓ ↓H␈↓␈↓↓O␈↓-players turn and ␈↓↓ocount␈↓ is 3 for some line.
␈↓ ↓H␈↓ ␈↓↓successors␈↓␈α
uses␈α
␈↓↓sort␈↓␈α
and␈α
its␈α
auxiliaries␈α
to␈α
prune␈α
and␈α
sort␈α
the␈α
list␈α
of␈α
possible␈α
moves␈α∞from␈α
a
␈↓ ↓H␈↓non-terminal␈αposition␈α
according␈αto␈α
the␈αfollowing␈α
rules.␈α If␈αsome␈α
move␈αis␈α
a␈αwin␈α
then␈αreturn␈α
a␈αlist
␈↓ ↓H␈↓containing␈α
only␈α
that␈α
move,␈α
otherwise␈α
if␈α
some␈αmove␈α
is␈α
required␈α
to␈α
keep␈α
the␈α
opponent␈αfrom␈α
winning
␈↓ ↓H␈↓then␈α
that␈αis␈α
returned␈α
as␈αthe␈α
single␈αpossibility,␈α
otherwise␈α
if␈αsome␈α
move␈αis␈α
a␈α
double-threat␈α(creates
␈↓ ↓H␈↓two␈αwinning␈αmoves␈αfor␈α
the␈αplayer)␈αthen␈αit␈αis␈α
returned␈αas␈αthe␈αonly␈α
possibility,␈αotherwise␈αa␈αlist␈αof␈α
all
␈↓ ↓H␈↓possible␈αmoves␈αis␈αreturned␈αwith␈αthe␈αthreats␈α(moves␈αcreating␈αa␈αwinning␈αmove␈αfor␈αthe␈αplayer)␈αat␈α
the
␈↓ ↓H␈↓front of the list.
␈↓ ↓H␈↓ The programs ␈↓↓revert␈↓ and ␈↓↓update␈↓ take back and make moves in the obvious way.
␈↓ ↓H␈↓ Here are the definitions: (note that the numbers are octal)
␈↓ ↓H␈↓␈↓ α8␈↓↓ commence[] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αprogram␈↓↓ []␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ array[lines, ␈↓¬T␈↓↓, 12]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ array[xcount, fixnum, 11]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ array[ocount, fixnum, 11]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ store[lines 1, ␈↓¬(1 4 7)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ store[lines 2, ␈↓¬(1 5)␈↓↓]␈↓
␈↓ ↓H␈↓8.1)␈↓ α8␈↓↓ store[lines 3, ␈↓¬(1 6 10)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ store[lines 4, ␈↓¬(2 4)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ store[lines 5, ␈↓¬(2 5 7 10)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ store[lines 6, ␈↓¬(2 6)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬}Chapter II␈↓ *45
␈↓ ↓H␈↓␈↓ α8␈↓↓ store[lines 7, ␈↓¬(3 4 10)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ store[lines 10, ␈↓¬(3 5)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ store[lines 11, ␈↓¬(3 6 7)␈↓↓]␈↓
␈↓ ↓H␈↓8.2)␈↓ α8␈↓↓ ext p ← ␈↓αa|␈↓↓p␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ newgame[] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αprogram␈↓↓ [n]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ n ← 0␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬L:␈↓↓ n ← add1 n␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ store[xcount n, 0]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ store[ocount n, 0]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ n < 10 ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L␈↓↓␈↓
␈↓ ↓H␈↓8.3)␈↓ α8␈↓↓ p1 ← ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ xs ← ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ os ← ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ bs ← ␈↓¬(1 2 3 4 5 6 7 10 11)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ w ← ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ level ← 0␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ count ← 0␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ return ␈↓¬(NEW GAME)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ter[p, alpha, beta] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ¬␈↓αn|␈↓↓p␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ∧ [level = 11␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ∨ 11 - level < alpha␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ∨ -11 + level > beta␈↓
␈↓ ↓H␈↓8.4)␈↓ α8␈↓↓ ∨ [␈↓αprogram␈↓↓ [n]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ n ← 0␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬L2:␈↓↓ n ← add1 n␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ 3 = [␈↓αif␈↓↓ w ␈↓αthen␈↓↓ xcount n ␈↓αelse␈↓↓ ocount n] ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ return ␈↓¬T␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ n < 10 ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L2␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ return ␈↓¬NIL␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ imval p ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ w ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [␈↓αprogram␈↓↓ [n]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ n ← 0␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬L3:␈↓↓ n ← add1 n␈↓
␈↓ ↓H␈↓8.5)␈↓ α8␈↓↓ ␈↓αif␈↓↓ 3 = xcount n ␈↓αthen␈↓↓ return 12 - level␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ n < 10 ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L3␈↓↓ ␈↓αelse␈↓↓ return 0]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αprogram␈↓↓ [n]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ n ← 0␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬L4:␈↓↓ n ← add1 n␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ 3 = ocount n ␈↓αthen␈↓↓ return -12 + level␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ n < 10 ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L4␈↓↓ ␈↓αelse␈↓↓ return 0␈↓
␈↓ ↓H␈↓46␈↓ ¬}Chapter II␈↓ H
␈↓ ↓H␈↓8.6)␈↓ α8␈↓↓ successors p ← sort mapcar[[λx: x . p], bs]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ revert[] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αprogram␈↓↓ [a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ level ← sub1 level␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ bs ← ␈↓αa|␈↓↓[␈↓αif␈↓↓ w ␈↓αthen␈↓↓ xs ␈↓αelse␈↓↓ os] . bs␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ w ␈↓αthen␈↓↓ xs ← ␈↓αd|␈↓↓xs ␈↓αelse␈↓↓ os ← ␈↓αd|␈↓↓os␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ a ← lines ␈↓αa|␈↓↓p1␈↓
␈↓ ↓H␈↓8.7)␈↓ α8␈↓↓ ␈↓¬L5:␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓a ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L6␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ w ␈↓αthen␈↓↓ store[xcount ␈↓αa|␈↓↓a, sub1 xcount ␈↓αa|␈↓↓a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ store[ocount ␈↓αa|␈↓↓a, sub1 ocount ␈↓αa|␈↓↓a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ a ← ␈↓αd|␈↓↓a␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L5␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬L6:␈↓↓ w ← ¬w␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ p1 ← ␈↓αd|␈↓↓p1␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ return[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ update m ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αprogram␈↓↓ [a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ level ← add1 level␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ w ␈↓αthen␈↓↓ os ← m . os ␈↓αelse␈↓↓ xs ← m . xs␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ bs ← delete[m, bs]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ p1 ← m . p1␈↓
␈↓ ↓H␈↓8.8)␈↓ α8␈↓↓ count ← add1 count␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ a ← lines m␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬L7:␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓a ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L8␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ w ␈↓αthen␈↓↓ store[ocount ␈↓αa|␈↓↓a, add1 ocount ␈↓αa|␈↓↓a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ store[xcount ␈↓αa|␈↓↓a, add1 xcount ␈↓αa|␈↓↓a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ a ← ␈↓αd|␈↓↓a␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αgo to␈↓↓ ␈↓¬L7␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬L8:␈↓↓ w ← ¬w␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ return[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ sort u ← sorta[u, ␈↓¬NIL␈↓↓, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ sorta[u, th, ord] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ [th * ord]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ win ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ <␈↓αa|␈↓↓u>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ answer ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ sortb[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ doubleth ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ sortc[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ threat ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ sorta[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u . th, ord]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ sorta[␈↓αd|␈↓↓u, th, ␈↓αa|␈↓↓u . ord]␈↓
␈↓ ↓H␈↓8.9)
␈↓ ↓H␈↓␈↓ α8␈↓↓ sortb[u, m] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ <m> ␈↓αelse␈↓↓ ␈↓αif␈↓↓ win ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ <␈↓αa|␈↓↓u> ␈↓αelse␈↓↓ sortb[␈↓αd|␈↓↓u, m]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ sortc[u, m] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ <m>␈↓
␈↓ ↓H␈↓␈↓ ¬}Chapter II␈↓ *47
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ win ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ <␈↓αa|␈↓↓u>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ answer ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ sortb[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ sortc[␈↓αd|␈↓↓u, m]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ win p ← ␈↓αif␈↓↓ w ␈↓αthen␈↓↓ orlis[[λx: 2 = ocount x], lines ␈↓αa|␈↓↓p]␈↓
␈↓ ↓H␈↓8.10)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ orlis[[λx: 2 = xcount x], lines ␈↓αa|␈↓↓p]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ answer p ← ␈↓
␈↓ ↓H␈↓8.11)␈↓ α8␈↓↓ ␈↓αif␈↓↓ w ␈↓αthen␈↓↓ orlis[[λx: 2 = xcount x], lines ␈↓αa|␈↓↓p]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ orlis[[λx: 2 = ocount x], lines ␈↓αa|␈↓↓p]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ doubleth p ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ twolis[[λx: ␈↓
␈↓ ↓H␈↓8.12)␈↓ α8␈↓↓ 1 = [␈↓αif␈↓↓ w ␈↓αthen␈↓↓ ocount x ␈↓αelse␈↓↓ xcount x]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ∧ orlis[[λw: x ε lines w], delete[␈↓αa|␈↓↓p, bs]]], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ lines ␈↓αa|␈↓↓p]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ twolis[pred, u] ← ␈↓
␈↓ ↓H␈↓8.13)␈↓ α8␈↓↓ ¬␈↓αn|␈↓↓u ∧ [[pred ␈↓αa|␈↓↓u ∧ orlis[pred, ␈↓αd|␈↓↓u]] ∨ twolis[pred, ␈↓αd|␈↓↓u]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ threat p ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ orlis[[λx: ␈↓
␈↓ ↓H␈↓8.14)␈↓ α8␈↓↓ 1 = [␈↓αif␈↓↓ w ␈↓αthen␈↓↓ ocount x ␈↓αelse␈↓↓ xcount x]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ∧ orlis[[λw: x ε lines w], delete[␈↓αa|␈↓↓p, bs]]], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ lines ␈↓αa|␈↓↓p]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ delete[x, u] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓8.15)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ x = ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ ␈↓αd|␈↓↓u␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . delete[x, ␈↓αd|␈↓↓u]␈↓
␈↓ ↓H␈↓ The␈α⊂following␈α⊂are␈α⊂the␈α⊂results␈α⊂of␈α⊂applying␈α∂the␈α⊂αβ-programs␈α⊂to␈α⊂a␈α⊂list␈α⊂containing␈α⊂a␈α∂single
␈↓ ↓H␈↓position (2 1). This means the first play has been in square 1 and the second play is to square 2.
␈↓ ↓H␈↓␈↓ β8␈↓↓valmin[␈↓¬((2 1)), -100, 100␈↓↓] = ␈↓¬3␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓valmax[␈↓¬((2 1)), -100, 100␈↓↓] = ␈↓¬0␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓linemin[␈↓¬((2 1)), NIL, -100, 100␈↓↓] = ␈↓¬(3 2 7 4 11 5 10)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓linemax[␈↓¬((2 1)), NIL, -100, 100␈↓↓] = ␈↓¬(0 2 11 5 10 7 3 6 4)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓treemin[␈↓¬((2 1)), NIL, NIL, -100, 100␈↓↓] = ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (3 ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ ((2 7 (4 11 (5 10 3))))␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (2 (3 10 (5 7 (11 3)))␈↓
␈↓ ↓H␈↓48␈↓ ¬}Chapter II␈↓ H
␈↓ ↓H␈↓␈↓ β8␈↓¬ (6 5 (10 7 (3 0)))␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (10 3 (7 4 (11 3)))␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (4 7 (5 11 (6 3)))␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (5 11 (7 4 (3 3)))␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (7 4 (11 5 (10 3)))␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (11 5 (10 7 (3 6 (4 0))))))␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓treemax[␈↓¬((2 1)), NIL, NIL, -100, 100␈↓↓] = ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (0 ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (2 (3 10 (5 7 (11 3)))␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (6 5 (10 7 (3 11 (4 0))))␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (10 3 (7 4 (11 3)))␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (4 7 (5 11 (6 3)))␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (5 11 (7 4 (3 3)))␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (7 4 (11 5 (10 3)))␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ (11 5 (10 7 (3 6 (4 0)))))␈↓
␈↓ ↓H␈↓␈↓ β8␈↓¬ ((2 11 (5 10 (7 3 (6 4 0)))))) ␈↓
␈↓ ↓H␈↓␈↓ εH␈↓ *49
␈↓ ↓H␈↓α␈↓ ¬zChapter III
␈↓ ↓H␈↓α␈↓ ∧(PROVING LISP PROGRAMS CORRECT
␈↓ ↓H␈↓ The␈αtheory␈αof␈αcomputation␈α
may␈αbe␈αdivided␈αinto␈αtwo␈α
parts.␈α The␈αfirst␈αis␈αthe␈α
general␈αtheory
␈↓ ↓H␈↓of␈α
computability␈α
including␈α
topics␈α
like␈α
universal␈α
functions,␈α
the␈α
existence␈α
of␈αuncomputable␈α
functions,
␈↓ ↓H␈↓lambda␈α∪calculus,␈α∪call-by-name␈α∪and␈α∪call-by-value,␈α∪and␈α∪the␈α∪relation␈α∪of␈α∪conditional␈α∩expression
␈↓ ↓H␈↓recursion␈α∞to␈α∞other␈α∞formalisms␈α∞for␈α∂describing␈α∞computable␈α∞functions.␈α∞ The␈α∞second␈α∞part,␈α∂which␈α∞we
␈↓ ↓H␈↓will␈α∂discuss␈α∞in␈α∂this␈α∞chapter␈α∂emphasizes␈α∞techniques␈α∂for␈α∞proving␈α∂particular␈α∞facts␈α∂about␈α∞particular
␈↓ ↓H␈↓computable␈α∂functions.␈α⊂ We␈α∂emphasize␈α∂the␈α⊂use␈α∂of␈α∂the␈α⊂techniques␈α∂more␈α∂than␈α⊂their␈α∂mathematical
␈↓ ↓H␈↓background.␈α Much␈αof␈αthe␈αmaterial␈αin␈αthis␈αchapter␈αis␈αbased␈αon␈α[Cartwright␈α1977]␈αand␈αthe␈αrest␈αon
␈↓ ↓H␈↓[McCarthy 1977].
␈↓ ↓H␈↓ We␈αshall␈αconfine␈αourselves␈αto␈αproving␈α␈↓↓extensional␈↓␈α␈↓↓properties␈↓␈αof␈α␈↓↓clean,␈↓␈α␈↓↓pure␈↓␈αLISP␈α
programs.
␈↓ ↓H␈↓An␈α⊂␈↓↓extensional␈↓␈α⊂␈↓↓property␈↓␈α⊂is␈α⊃one␈α⊂that␈α⊂depends␈α⊂only␈α⊂on␈α⊃the␈α⊂function␈α⊂computed␈α⊂by␈α⊃the␈α⊂program.
␈↓ ↓H␈↓Thus␈α∞it␈α∞includes␈α∞the␈α∞fact␈α∂that␈α∞two␈α∞sort␈α∞programs␈α∞compute␈α∂the␈α∞same␈α∞function␈α∞or␈α∞that␈α∂␈↓↓append␈↓␈α∞is
␈↓ ↓H␈↓associative␈αbut␈αdoes␈αnot␈αinclude␈αthe␈αfact␈αthat␈αone␈αsort␈αprogram␈αdoes␈α␈↓↓n␈↓∧2␈↓↓␈↓␈αcomparisons␈αand␈αanother
␈↓ ↓H␈↓␈↓↓n log n␈↓␈α
comparisons.␈α
␈↓↓Clean␈↓␈α
LISP␈α
programs␈α
have␈α
no␈α
side␈α
effects␈α
(our␈α
methods␈α
require␈αthe␈α
freedom
␈↓ ↓H␈↓to␈α
replace␈αa␈α
subexpression␈αby␈α
an␈αequal␈α
expression),␈αand␈α
equality␈αrefers␈α
to␈αthe␈α
S-expressions␈αand
␈↓ ↓H␈↓not␈αto␈αthe␈αlist␈αstructures.␈α ␈↓↓Pure␈↓␈αLISP␈αinvolves␈αonly␈αrecursive␈αfunction␈αdefinitions␈αand␈α
doesn't␈αuse
␈↓ ↓H␈↓assignment␈αstatements.␈α
It␈αwouldn't␈α
be␈αdifficult␈α
to␈αgive␈α
corresponding␈αmethods␈α
that␈αwould␈αbe␈α
valid
␈↓ ↓H␈↓for non-functional programs, because this topic is well developed.
␈↓ ↓H␈↓ In␈α⊂spite␈α⊂of␈α∂all␈α⊂these␈α⊂limitations,␈α⊂the␈α∂techniques␈α⊂are␈α⊂useful␈α⊂and␈α∂point␈α⊂the␈α⊂way␈α⊂to␈α∂further
␈↓ ↓H␈↓progress.␈α∞ The␈α∞main␈α∞idea␈α∂is␈α∞to␈α∞represent␈α∞the␈α∞programs␈α∂formally␈α∞using␈α∞first␈α∞order␈α∂logic.␈α∞ Higher
␈↓ ↓H␈↓order␈α∞functionals␈α∂and␈α∞predicates␈α∞are␈α∂used␈α∞informally.␈α∞ Formal␈α∂proofs␈α∞of␈α∞properties␈α∂of␈α∞programs
␈↓ ↓H␈↓can␈α∀easily␈α∀be␈α∀checked␈α∀using␈α∀an␈α∀automatic␈α∀proof␈α∀checker␈α∀such␈α∀as␈α∀FOL␈α∀[Weyhrauch␈α∪1977].
␈↓ ↓H␈↓Examples are given in Appendix A.
␈↓ ↓H␈↓ We␈αbegin␈αwith␈αan␈αinformal␈αsample␈αproof,␈αcommenting␈αabout␈αthe␈αinformation␈α
and␈αconcepts
␈↓ ↓H␈↓needed␈αto␈αmake␈αthe␈αproof␈αwork.␈α We␈αthen␈αproceed␈αto␈αformalize␈αthese␈αideas.␈α The␈α necessary␈αbasic
␈↓ ↓H␈↓facts␈αcan␈αbe␈αdivided␈αinto␈αseveral␈αcategories:␈αfirst␈αorder␈αlogic␈αincluding␈αconditional␈αforms␈αand␈αfirst
␈↓ ↓H␈↓order␈αlambda-expressions,␈αalgebraic␈αfacts␈αabout␈αlists␈αand␈αS-expressions,␈αfacts␈αabout␈αthe␈αinductive
␈↓ ↓H␈↓structure of lists and S-expressions, and general facts about functions defined by recursion.
␈↓ ↓H␈↓1. ␈↓αIntroductory Example.␈↓
␈↓ ↓H␈↓ Consider the function ␈↓↓append␈↓ ( * ) defined by
␈↓ ↓H␈↓␈↓ ∧G␈↓↓u * v ← if ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]␈↓
␈↓ ↓H␈↓and the predicate ␈↓↓member␈↓ ( ε ) defined by
␈↓ ↓H␈↓␈↓ ∧L␈↓↓x ε u ← ¬␈↓αn|␈↓↓u ∧ [x = ␈↓αa|␈↓↓u ∨ x ε ␈↓αd|␈↓↓u] ␈↓
␈↓ ↓H␈↓where␈α
␈↓↓x␈↓␈α
is␈α
any␈α
S-expression␈α
and␈α
␈↓↓u␈↓␈α
and␈α
␈↓↓v␈↓␈α
are␈α
lists␈α
(whose␈α
elements␈α
are␈α
S-expresssions).␈α
(Note␈α
that
␈↓ ↓H␈↓50␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓our␈αprecedence␈αconventions␈α
imply␈αthat␈α ␈↓↓␈↓αd|␈↓↓u * v = [␈↓αd|␈↓↓u] * v␈↓.)␈α
It␈αis␈αprobably␈α
intuitively␈αclear␈αthat␈α
if
␈↓ ↓H␈↓␈↓↓x␈↓␈α
is␈αa␈α
member␈αof␈α
one␈αof␈α
the␈αlists␈α
␈↓↓u␈↓␈αor␈α
␈↓↓v␈↓␈α
then␈α␈↓↓x␈↓␈α
is␈αa␈α
member␈αof␈α
the␈αlist␈α
␈↓↓u * v␈↓.␈α We␈α
will␈α
give␈αan
␈↓ ↓H␈↓informal proof of this property.
␈↓ ↓H␈↓ The␈α∂proof␈α∞will␈α∂be␈α∞based␈α∂on␈α∂the␈α∞principle␈α∂of␈α∞␈↓↓list induction␈↓␈α∂which␈α∞is␈α∂analogous␈α∂to␈α∞natural
␈↓ ↓H␈↓induction␈α∂(for␈α⊂functions␈α∂on␈α∂the␈α⊂natural␈α∂numbers).␈α⊂ This␈α∂principle␈α∂states␈α⊂that␈α∂if␈α⊂some␈α∂property,
␈↓ ↓H␈↓␈↓πF␈↓(␈↓¬NIL␈↓),␈αholds␈αfor␈αthe␈αlist␈α
␈↓¬NIL␈↓␈αand␈αif,␈αby␈αassuming␈α
␈↓πF␈↓(␈↓↓u␈↓)␈αholds␈αfor␈α␈↓↓␈↓αd|␈↓↓u␈↓,␈αwe␈α
can␈αprove␈αthat␈αit␈αholds␈α
for
␈↓ ↓H␈↓the list ␈↓↓u␈↓ then ␈↓πF␈↓(␈↓↓u␈↓) holds for all lists ␈↓↓u.␈↓ In our case ␈↓πF␈↓(␈↓↓u␈↓) is the property:
␈↓ ↓H␈↓␈↓ ∧G␈↓↓[x ε u ∨ x ε v] ⊃ x ε [u * v] . ␈↓
␈↓ ↓H␈↓The␈αinduction␈αwill␈αbe␈αwith␈αrespect␈αto␈αthe␈αlist␈α␈↓↓u,␈↓␈αeg.␈αthe␈αfirst␈αargument␈αto␈α␈↓↓append.␈↓␈α The␈αvariables
␈↓ ↓H␈↓␈↓↓x␈↓ and ␈↓↓v␈↓ are just carried along.
␈↓ ↓H␈↓ The␈αproof␈αconsists␈αof␈αtwo␈αparts.␈α First␈αwe␈αassume␈α␈↓αn|␈↓␈↓↓u.␈↓␈α Then␈αby␈αthe␈αdefinition␈α
of␈α␈↓↓member,␈↓
␈↓ ↓H␈↓␈↓↓x ε u␈↓ is false and by the definition of ␈↓↓append,␈↓ ␈↓↓u * v = v␈↓. Thus ␈↓πF␈↓(␈↓↓u␈↓) becomes
␈↓ ↓H␈↓␈↓ ¬Q␈↓↓x ε v ⊃ x ε v ␈↓
␈↓ ↓H␈↓which is true.
␈↓ ↓H␈↓ Next we assume ¬␈↓αn|␈↓␈↓↓u␈↓ and that ␈↓↓␈↓πF␈↓↓(␈↓αd|␈↓↓u)␈↓ is true. Thus
␈↓ ↓H␈↓␈↓ ¬∧␈↓↓[x ε ␈↓αd|␈↓↓u ∨ x ε v] ⊃ x ε [␈↓αd|␈↓↓u * v]␈↓
␈↓ ↓H␈↓By␈αdefinition␈α
of␈α␈↓↓append,␈↓␈α
␈↓↓u * v␈↓␈αreduces␈α
to␈α␈↓↓␈↓αa|␈↓↓u␈α
.␈α[␈↓αd|␈↓↓u␈α
*␈αv]␈↓.␈α
Thus␈α␈↓↓u * v ≠ ␈↓¬NIL␈↓↓␈↓,␈α
␈↓↓␈↓αa|␈↓↓[u * v] = ␈↓αa|␈↓↓u␈↓␈αand
␈↓ ↓H␈↓␈↓↓␈↓αd|␈↓↓[u * v] = ␈↓αd|␈↓↓u * v␈↓.␈α∞ By␈α∂definition␈α∞of␈α∞␈↓↓member,␈↓␈α∂␈↓↓x ε u␈↓␈α∞reduces␈α∞to␈α∂␈↓↓x = ␈↓αa|␈↓↓u ∨ x ε ␈↓αd|␈↓↓u␈↓.␈α∞ So␈α∂we␈α∞consider
␈↓ ↓H␈↓two cases: ␈↓↓x = ␈↓αa|␈↓↓u␈↓ and ␈↓↓x ε ␈↓αd|␈↓↓u ∨ x ε v␈↓.
␈↓ ↓H␈↓ In␈αthe␈αfirst␈α
case␈αwe␈αhave␈α␈↓↓x = ␈↓αa|␈↓↓[u * v]␈↓␈α
and␈αso,␈αby␈αdefinition␈α
of␈α␈↓↓member,␈↓␈α␈↓↓x ε [u * v]␈↓␈αand␈α
␈↓πF␈↓(␈↓↓u␈↓)
␈↓ ↓H␈↓is␈α
true.␈α In␈α
the␈α
second␈αcase␈α
we␈α
have␈αby␈α
the␈αinduction␈α
hypothesis␈α
that␈α␈↓↓x ε [␈↓αd|␈↓↓u * v]␈↓␈α
and␈α
again,␈αby
␈↓ ↓H␈↓definition of ␈↓↓member,␈↓ ␈↓πF␈↓(␈↓↓u␈↓) holds.
␈↓ ↓H␈↓ Thus␈α
we␈αhave␈α
done␈α
the␈αrequired␈α
proofs␈αand␈α
by␈α
list␈αinduction␈α
the␈αproperty␈α
␈↓πF␈↓(␈↓↓u␈↓)␈α
holds␈αfor
␈↓ ↓H␈↓any list ␈↓↓u.␈↓
␈↓ ↓H␈↓ In carrying out the proof we have used properties of lists such as
␈↓ ↓H␈↓ ␈↓↓[x . u]␈↓ is a list
␈↓ ↓H␈↓ ␈↓↓␈↓αa|␈↓↓[x . u] = x]␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αd|␈↓↓[x . u] = u]␈↓
␈↓ ↓H␈↓ ␈↓↓¬␈↓αn|␈↓↓[x . u]␈↓
␈↓ ↓H␈↓for␈α
any␈α
S-expression␈α
␈↓↓x␈↓␈α∞and␈α
any␈α
list␈α
␈↓↓u.␈↓␈α∞ We␈α
have␈α
also␈α
used␈α∞these␈α
properties␈α
with␈α
␈↓↓u␈↓␈α∞replaced␈α
by
␈↓ ↓H␈↓␈↓↓u * v␈↓␈αor␈α␈↓↓␈↓αd|␈↓↓u * v␈↓.␈α To␈αdo␈αthis␈αwe␈αneed␈αto␈αknow␈αthat␈α␈↓↓u * v␈↓␈αis␈αa␈αlist␈αor␈αthat␈α␈↓↓␈↓αd|␈↓↓u * v␈↓␈αis␈αa␈αlist.␈α This␈αis
␈↓ ↓H␈↓essentially␈α⊂the␈α⊂same␈α⊂as␈α⊃saying␈α⊂that␈α⊂the␈α⊂computation␈α⊂defined␈α⊃by␈α⊂␈↓↓append␈↓␈α⊂terminates␈α⊂in␈α⊃a␈α⊂finite
␈↓ ↓H␈↓number of steps or that ␈↓↓append␈↓ is a total function.
␈↓ ↓H␈↓ Saying␈αthat␈α␈↓↓u␈α*␈α
v␈↓␈αreduces␈αto␈α␈↓↓␈↓αa|␈↓↓u␈α.␈α
[␈↓αd|␈↓↓u␈α*␈αv]␈↓␈αis␈αbased␈α
on␈αthe␈αrule␈αfor␈α
computing␈αrecursively
␈↓ ↓H␈↓defined␈α∂functions.␈α∂ It␈α∂too,␈α∂is␈α∂only␈α∂meaningful␈α∂when␈α∞␈↓↓␈↓αd|␈↓↓u * v␈↓␈α∂is␈α∂a␈α∂list,␈α∂eg.␈α∂when␈α∂␈↓↓append␈↓␈α∂is␈α∞total.
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *51
␈↓ ↓H␈↓Note␈αthat␈αin␈αusing␈αthe␈αrecursive␈αfunction␈α
definition␈αand␈αthe␈αrule␈αfor␈αcomptation␈αwe␈αare␈α
essentially
␈↓ ↓H␈↓substituting␈α∪equals␈α∩for␈α∪equals␈α∪in␈α∩the␈α∪problem.␈α∪ The␈α∩two␈α∪cases␈α∪can␈α∩be␈α∪combined␈α∪giving␈α∩the
␈↓ ↓H␈↓␈↓↓functional equation␈↓ for ␈↓↓append␈↓
␈↓ ↓H␈↓␈↓ ∧@␈↓↓u * v = ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v].␈↓
␈↓ ↓H␈↓Again, being able to use the functional equation here depends on knowing ␈↓↓append␈↓ is total.
␈↓ ↓H␈↓ Similiarly␈α
several␈α
of␈αthe␈α
statements␈α
made␈α
about␈α␈↓↓member␈↓␈α
depend␈α
on␈α
the␈αfact␈α
that␈α
it␈α
is␈αtotal.
␈↓ ↓H␈↓Thus␈α
to␈α
complete␈α
the␈α
proof␈α
it␈α
is␈α
necessary␈α
to␈α
know␈α
that␈α
␈↓↓append␈↓␈α
and␈α
␈↓↓member␈↓␈α
are␈α
total.␈α
This␈α
should
␈↓ ↓H␈↓be␈α⊂intuitively␈α⊂clear,␈α⊂since␈α⊂the␈α⊂recursive␈α⊂calls␈α⊂in␈α⊂the␈α⊂definitions␈α⊂ depend␈α⊂on␈α⊂the␈α⊂␈↓↓cdr␈↓␈α⊂of␈α⊂the␈α∂test
␈↓ ↓H␈↓variable␈α∩and,␈α∩since␈α⊃our␈α∩lists␈α∩are␈α∩finite,␈α⊃this␈α∩will␈α∩eventually␈α∩be␈α⊃␈↓¬NIL␈↓␈α∩and␈α∩the␈α∩computation␈α⊃will
␈↓ ↓H␈↓terminate. We will see later how to state and prove such facts.
␈↓ ↓H␈↓ The␈αvalidity␈αof␈αthe␈αlist␈αinduction␈αprinciple␈αis␈αdue␈αto␈αthe␈αfact␈αthat␈αwe␈αrequire␈αour␈αlists␈αto␈αbe
␈↓ ↓H␈↓finite.␈α This␈αjust␈αis␈αone␈αof␈αmany␈αforms␈αof␈αinduction.␈α We␈αwill␈αsee␈αexamples␈αof␈αother␈αforms␈αas␈αwell
␈↓ ↓H␈↓as␈αother␈αapplications␈αof␈αlist␈αinduction.␈α Looking␈α
at␈αinduction␈αin␈αanother␈αway,␈αif␈αone␈αstarts␈α
with␈αa
␈↓ ↓H␈↓set␈α∞of␈α∞properties␈α∞that␈α∞must␈α∞be␈α∞satisfied␈α∞by␈α∞a␈α∞domain␈α∞and␈α∞some␈α∞basic␈α∞functions␈α∞on␈α∞that␈α
domain,
␈↓ ↓H␈↓then␈α∂requiring␈α∂that␈α∂some␈α∂induction␈α∂principle␈α∂be␈α∞valid␈α∂ is␈α∂a␈α∂way␈α∂of␈α∂giving␈α∂further␈α∞information
␈↓ ↓H␈↓about the domain.
␈↓ ↓H␈↓2. ␈↓αFirst Order Logic with Conditional Forms and Lambda-expressions.␈↓
␈↓ ↓H␈↓ The␈α∂logic␈α∂we␈α∂shall␈α∂use␈α∂is␈α∂a␈α∂sorted␈α∂first␈α∂order␈α∂logic␈α∂with␈α∂equality,␈α∂extended␈α∂by␈α∞admitting
␈↓ ↓H␈↓conditional␈α∀forms␈α∪as␈α∀terms␈α∀and␈α∪first␈α∀order␈α∀lambda-expressions␈α∪as␈α∀function␈α∀symbols.␈α∪ These
␈↓ ↓H␈↓extensions␈αdo␈α
not␈αchange␈α
the␈αlogical␈α
strength␈αof␈α
the␈αtheory,␈α
because,␈αas␈α
we␈αshall␈α
see,␈αevery␈α
formula
␈↓ ↓H␈↓that␈α⊂includes␈α⊂conditional␈α⊂forms␈α⊂or␈α⊂first␈α⊂order␈α⊂lambdas␈α⊂can␈α⊂be␈α⊂transformed␈α⊂into␈α⊂an␈α∂equivalent
␈↓ ↓H␈↓formula␈αwithout␈αthem.␈α However,␈αthe␈αextensions␈αare␈αpractically␈αimportant,␈αbecause␈αthey␈αpermit␈αus
␈↓ ↓H␈↓to␈α∞use␈α∞recursive␈α
definitions␈α∞directly␈α∞as␈α
formulas␈α∞of␈α∞the␈α
logic.␈α∞ The␈α∞adjective␈α
sorted␈α∞refers␈α∞to␈α
the
␈↓ ↓H␈↓fact␈αthat␈αwe␈αmay␈αrestrict␈αa␈αvariable␈αto␈αrange␈αover␈αsome␈αtype␈αof␈αobject␈α(a␈αsort)␈αrather␈αthan␈αover␈αall
␈↓ ↓H␈↓objects.␈α∞We␈α∞can␈α∞then␈α
restrict␈α∞the␈α∞domain␈α∞of␈α
a␈α∞function␈α∞to␈α∞some␈α
fixed␈α∞sort.␈α∞ This␈α∞means␈α∞that␈α
in
␈↓ ↓H␈↓proving␈α∞properties␈α∞of␈α∞that␈α∞function␈α∞attention␈α∞can␈α∞be␈α∞focused␈α∞on␈α∞the␈α∞domain␈α∞of␈α∂interest␈α∞without
␈↓ ↓H␈↓worrying␈α
about␈αwhat␈α
happens␈αfor␈α
arguments␈αfor␈α
which␈αthe␈α
function␈αis␈α
not␈αintended␈α
to␈αbe␈α
defined.
␈↓ ↓H␈↓For␈αexample,␈αwe␈αshall␈αoften␈αuse␈αthe␈αvariables␈α␈↓↓u␈↓␈αand␈α␈↓↓v␈↓␈αto␈αdenote␈αlists,␈αand␈αthe␈αsorting␈αwill␈αpermit
␈↓ ↓H␈↓us␈αto␈αwrite␈αstatements␈αthat␈αare␈αtrue␈αfor␈αlists␈αbut␈αnot␈αfor␈αgeneral␈αS-expressions␈αwithout␈αputting␈αthe
␈↓ ↓H␈↓fact that their validity is restricted to lists into the statement itself.
␈↓ ↓H␈↓ The␈α⊂language␈α∂for␈α⊂our␈α⊂logic␈α∂contains␈α⊂terms,␈α⊂formulas,␈α∂function␈α⊂expressions␈α⊂and␈α∂predicate
␈↓ ↓H␈↓expressions␈α∂which␈α⊂are␈α∂built␈α⊂from␈α∂constants,␈α⊂variables,␈α∂predicate␈α⊂symbols,␈α∂and␈α⊂function␈α∂symbols
␈↓ ↓H␈↓using function application, conditional terms, boolean terms, lambda expressions, and quantifiers.
␈↓ ↓H␈↓␈↓αConstants␈↓:␈α∞We␈α
will␈α∞use␈α∞S-expresssions␈α
as␈α∞constants␈α
standing␈α∞for␈α∞themselves␈α
and␈α∞also␈α∞lower␈α
case
␈↓ ↓H␈↓letters␈α∩from␈α∩the␈α∩first␈α∩part␈α∩of␈α∩the␈α∪alphabet␈α∩to␈α∩represent␈α∩constants␈α∩in␈α∩other␈α∩domains␈α∪than␈α∩S-
␈↓ ↓H␈↓expressions.␈α In␈αparticular␈αwe␈αwill␈αhave␈αthe␈αatom␈α␈↓¬NIL␈↓.␈α Other␈αatoms␈αwill␈αbe␈αintroduced␈αas␈αneeded.
␈↓ ↓H␈↓Among the non-S-expression constants we will have ␈↓π|␈↓ (read "bottom").
␈↓ ↓H␈↓␈↓αVariables␈↓:␈αWe␈αwill␈αuse␈αthe␈αletters␈α␈↓↓XX␈↓␈αthrough␈α␈↓↓ZZ␈↓␈αas␈αgeneral␈αvariables.␈α The␈αvariables␈α␈↓↓u␈↓␈αthrough
␈↓ ↓H␈↓52␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓␈↓↓w␈↓␈α will␈αrange␈αover␈αlists,␈αwhile␈α␈↓↓x␈↓␈αthrough␈α␈↓↓z␈↓␈αwill␈αrange␈αover␈αS-expressions␈αand␈α␈↓↓X␈↓␈αthrough␈α␈↓↓Z␈↓␈αrange
␈↓ ↓H␈↓over␈αextended␈αS-expressions␈α(the␈αset␈αconsisting␈αof␈αS-expressions␈αtogther␈αwith␈αthe␈αconstant␈α␈↓π|␈↓).␈α ␈↓↓k␈↓
␈↓ ↓H␈↓through␈α
␈↓↓n␈↓␈αare␈α
integer␈α
variables.␈α Variables␈α
ranging␈α
over␈αother␈α
sorts␈α
will␈αbe␈α
introduced␈αlater,␈α
when
␈↓ ↓H␈↓it is meaningful. The variables may appear with or without subscripts.
␈↓ ↓H␈↓␈↓αFunction␈αsymbols␈↓:␈α As␈αfunction␈αconstant␈αsymbols␈αwe␈αwill␈αuse␈αthe␈αLISP␈αfunction␈αnames␈α␈↓αa␈↓,␈α␈↓αd␈↓␈αand␈α.
␈↓ ↓H␈↓(as␈αan␈αinfix).␈α Other␈αfunction␈αsymbols␈αincluding␈αfunction␈αparameter␈αsymbols␈αwill␈αbe␈αintroduced␈α
as
␈↓ ↓H␈↓needed.␈α∂ We␈α⊂suppose␈α∂that␈α∂each␈α⊂function␈α∂symbol␈α⊂takes␈α∂the␈α∂same␈α⊂definite␈α∂number␈α⊂of␈α∂arguments
␈↓ ↓H␈↓every␈α∞time␈α∂it␈α∞is␈α∂used.␈α∞ Thus␈α∞if␈α∂the␈α∞same␈α∂letter␈α∞is␈α∞used␈α∂with␈α∞differing␈α∂numbers␈α∞of␈α∂arguments,␈α∞it
␈↓ ↓H␈↓stands␈α⊃for␈α∩different␈α⊃function␈α∩symbols.␈α⊃ Thus␈α∩we␈α⊃can␈α∩use␈α⊃␈↓↓<x␈↓β1␈↓↓,␈α∩...␈α⊃,x␈↓βn␈↓↓>␈↓␈α∩to␈α⊃represent␈α∩the␈α⊃list
␈↓ ↓H␈↓forming␈α
functions,␈α
but␈αwe␈α
shall␈α
have␈αto␈α
axiomatize␈α
it␈αseparately␈α
for␈α
each␈αlength␈α
we␈α
actually␈αuse.
␈↓ ↓H␈↓We␈αwill␈αoften␈αuse␈αone␈α
argument␈αfunctions␈αsymbols␈αas␈αprefixes,␈α
i.e.␈α without␈αbrackets,␈αjust␈αas␈α␈↓αa␈↓␈α
and
␈↓ ↓H␈↓␈↓αd␈↓ have been used up to now.
␈↓ ↓H␈↓␈↓αPredicate␈α
symbols␈↓:␈α
As␈α
predicate␈αconstant␈α
symbols␈α
we␈α
will␈α
use␈αthe␈α
logical␈α
equality␈α
symbol␈α
=,␈αand
␈↓ ↓H␈↓the␈αLISP␈αpredicate␈αnames␈α␈↓αat␈↓,␈α␈↓αn␈↓␈αand␈α␈↓αeq␈↓.␈α We␈αwill␈αuse␈α␈↓πF␈↓␈αas␈αa␈αpredicate␈αparameter␈αsymbol.␈αCertain
␈↓ ↓H␈↓unary␈α∞predicate␈α∞symbols␈α∞will␈α∞be␈α∞designated␈α∞as␈α∞sorts.␈α∞In␈α∞particular␈α∞␈↓↓isesexp,␈↓␈α∞␈↓↓issexp␈↓␈α∞and␈α∂␈↓↓islist␈↓␈α∞are
␈↓ ↓H␈↓sorts.␈α Other␈αpredicate␈αsymbols␈αand␈αsorts␈αwill␈αbe␈αintroduced␈αwhen␈αneeded.␈α We␈αsuppose␈αthat␈αeach
␈↓ ↓H␈↓predicate␈α
symbol␈αtakes␈α
the␈α
same␈αdefinite␈α
number␈αof␈α
arguments␈α
each␈αtime␈α
it␈α
is␈αused.␈α
Again␈αa␈α
letter
␈↓ ↓H␈↓may␈α∞be␈α∂used␈α∞with␈α∞differing␈α∂numbers␈α∞of␈α∂arguments␈α∞to␈α∞represent␈α∂different␈α∞predicates.␈α∂ Infix␈α∞and
␈↓ ↓H␈↓prefix notation will also be used where this is customary.
␈↓ ↓H␈↓ Next␈αwe␈αdefine␈αterms,␈αformulas,␈αfunction␈αexpressions␈αand␈αpredicate␈αexpressions␈αinductively.
␈↓ ↓H␈↓A␈αterm␈αis␈αan␈αexpression␈αwhose␈αvalue␈αwill␈αbe␈αan␈αobject␈αlike␈αan␈αS-expression␈αor␈αa␈αnumber␈αwhile␈αa
␈↓ ↓H␈↓formula␈α⊂has␈α⊂value␈α⊂␈↓αtrue␈↓␈α⊂or␈α∂␈↓αfalse␈↓.␈α⊂ We␈α⊂will␈α⊂use␈α⊂the␈α⊂symbols␈α∂T␈α⊂and␈α⊂F␈α⊂for␈α⊂the␈α⊂formulas␈α∂always
␈↓ ↓H␈↓having␈αthe␈αvalues␈α␈↓αtrue␈↓␈αand␈α␈↓αfalse␈↓␈αrespectively.␈α Terms␈αare␈αused␈αin␈αmaking␈αformulas,␈αand␈αformulas
␈↓ ↓H␈↓occur␈αin␈αterms␈αso␈αthat␈αthe␈αdefinitions␈αare␈α␈↓↓mutually␈↓␈α␈↓↓recursive␈↓␈αwhere␈αthis␈αuse␈αof␈αthe␈α
word␈α␈↓↓recursive␈↓
␈↓ ↓H␈↓should␈αbe␈αdistinguished␈αfrom␈αits␈αuse␈α
in␈αrecursive␈αdefinitions␈αof␈αfunctions.␈α Function␈αand␈α
predicate
␈↓ ↓H␈↓expressions are also involved in the mutual recursion.
␈↓ ↓H␈↓␈↓αTerms␈↓:␈α⊂Constants␈α⊃are␈α⊂terms,␈α⊃and␈α⊂variables␈α⊃are␈α⊂terms.␈α⊃ If␈α⊂␈↓↓f␈↓␈α⊃is␈α⊂a␈α⊃function␈α⊂expression␈α⊃taking␈α⊂␈↓↓n␈↓
␈↓ ↓H␈↓arguments,␈αand␈α
␈↓↓t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓␈↓␈αare␈α
terms,␈αthen␈α
␈↓↓f[t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓]␈↓␈αis␈αa␈α
term.␈α If␈α
␈↓↓s␈↓␈αis␈α
a␈αformula␈α
and␈α␈↓↓t␈↓β1␈↓↓␈↓␈αand␈α
␈↓↓t␈↓β2␈↓↓␈↓
␈↓ ↓H␈↓are␈α∞terms,␈α∞then␈α∞␈↓↓␈↓αif␈↓↓ s ␈↓αthen␈↓↓ t␈↓β1␈↓↓ ␈↓αelse␈↓↓ t␈↓β2␈↓↓␈↓␈α
is␈α∞a␈α∞term.␈α∞ We␈α∞soften␈α
the␈α∞notation␈α∞by␈α∞allowing␈α∞infix␈α
symbols
␈↓ ↓H␈↓where this is customary.
␈↓ ↓H␈↓␈↓αFormulas␈↓:␈α∂T␈α∂and␈α∞F␈α∂are␈α∂formulas.␈α∞ If␈α∂␈↓↓t␈↓β1␈↓↓␈↓␈α∂and␈α∂␈↓↓t␈↓β2␈↓↓␈↓␈α∞are␈α∂terms␈α∂then␈α∞␈↓↓t␈↓β1␈↓↓ = t␈↓β2␈↓↓␈↓␈α∂is␈α∂a␈α∞formula.␈α∂ If␈α∂␈↓πf␈↓␈α∂is␈α∞a
␈↓ ↓H␈↓predicate␈α∪expression␈α∪taking␈α∪␈↓↓n␈↓␈α∪arguments␈α∪and␈α∩␈↓↓t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓␈↓␈α∪are␈α∪terms,␈α∪then␈α∪␈↓↓␈↓πf␈↓↓[t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓]␈↓␈α∪is␈α∩a
␈↓ ↓H␈↓formula.␈α∞ If␈α∞␈↓↓s␈↓␈α∞is␈α∞a␈α∞formula,␈α∞then␈α∞¬␈↓↓s␈↓␈α∞is␈α
also␈α∞a␈α∞formula.␈α∞ If␈α∞␈↓↓s␈↓β1␈↓↓␈↓␈α∞and␈α∞␈↓↓s␈↓β2␈↓↓␈↓␈α∞are␈α∞formulas,␈α∞then␈α
␈↓↓s␈↓β1␈↓↓␈↓ ∧ ␈↓↓s␈↓β2␈↓↓␈↓,
␈↓ ↓H␈↓␈↓↓s␈↓β1␈↓↓␈↓ ∨ ␈↓↓s␈↓β2␈↓↓␈↓,␈α␈↓↓s␈↓β1␈↓↓␈↓ ⊃ ␈↓↓s␈↓β2␈↓↓␈↓,␈αand␈α␈↓↓s␈↓β1␈↓↓␈↓ ≡ ␈↓↓s␈↓β2␈↓↓␈↓␈αare␈αformulas.␈α If␈α␈↓↓s␈↓β0␈↓↓␈↓,␈α␈↓↓s␈↓β1␈↓↓␈↓␈αand␈α␈↓↓s␈↓β2␈↓↓␈↓␈αare␈αformulas,␈α
then␈α␈↓↓␈↓αif␈↓↓ s␈↓β0␈↓↓ ␈↓αthen␈↓↓ s␈↓β1␈↓↓ ␈↓αelse␈↓↓ s␈↓β2␈↓↓␈↓
␈↓ ↓H␈↓is␈α⊗a␈α⊗formula.␈α⊗ If␈α⊗␈↓↓x␈↓β1␈↓↓, ..., x␈↓βn␈↓↓␈↓␈α⊗are␈α⊗variables,␈α⊗and␈α⊗␈↓↓s␈↓␈α⊗is␈α⊗a␈α⊗formula,␈α⊗then␈α⊗␈↓↓[∃x␈↓β1␈↓↓ ... x␈↓βn␈↓↓: s]␈↓␈α⊗and
␈↓ ↓H␈↓␈↓↓[∀x␈↓β1␈↓↓ ... x␈↓βn␈↓↓: s]␈↓ are formulas.
␈↓ ↓H␈↓␈↓αFunction␈α
expressions␈↓:␈α∞A␈α
function␈α
symbol␈α∞is␈α
a␈α
function␈α∞expression.␈α
If␈α
␈↓↓x␈↓β1␈↓↓, ... ,x␈↓βn␈↓↓␈↓␈α∞are␈α
variables
␈↓ ↓H␈↓and ␈↓↓t␈↓ is a term, then ␈↓↓[λx␈↓β1␈↓↓, ... ,x␈↓βn␈↓↓: t]␈↓ is a function expression.
␈↓ ↓H␈↓␈↓αPredicate␈αexpressions␈↓:␈αA␈αpredicate␈αsymbol␈αis␈αa␈αpredicate␈αexpression.␈α If␈α␈↓↓x␈↓β1␈↓↓, ... ,x␈↓βn␈↓↓␈↓␈αare␈αvariables
␈↓ ↓H␈↓and ␈↓↓s␈↓ is a formula, then ␈↓↓[λx␈↓β1␈↓↓, ... ,x␈↓βn␈↓↓: s]␈↓ is a predicate expression.
␈↓ ↓H␈↓ An␈αoccurrence␈αof␈α
a␈αvariable␈α␈↓↓x␈↓␈α
is␈αcalled␈α␈↓↓bound␈↓␈α
if␈αit␈αis␈α
in␈αan␈αexpression␈α
of␈αone␈αof␈α
the␈αforms
␈↓ ↓H␈↓␈↓↓[λx␈↓β1␈↓↓ ... x␈↓βn␈↓↓: t]␈↓,␈α␈↓↓[λx␈↓β1␈↓↓ ... x␈↓βn␈↓↓: s]␈↓,␈α␈↓↓[∀x␈↓β1␈↓↓ ... x␈↓βn␈↓↓: s]␈↓␈αor␈α␈↓↓[∃x␈↓β1␈↓↓ ... x␈↓βn␈↓↓: s]␈↓␈αwhere␈α␈↓↓x␈↓␈αis␈αone␈αof␈αthe␈αnumbered␈α␈↓↓x␈↓'s.
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *53
␈↓ ↓H␈↓If␈α∩not␈α⊃bound␈α∩an␈α⊃occurrence␈α∩is␈α⊃called␈α∩␈↓↓free.␈↓␈α⊃ A␈α∩formula␈α⊃having␈α∩no␈α⊃free␈α∩variables␈α⊃is␈α∩called␈α⊃a
␈↓ ↓H␈↓␈↓↓sentence.␈↓
␈↓ ↓H␈↓ The␈α␈↓↓semantics␈↓␈αof␈αfirst␈αorder␈αlogic␈αconsists␈αof␈αthe␈αrules␈αthat␈αdetermine␈αwhether␈αa␈αformula␈αis
␈↓ ↓H␈↓␈↓αtrue␈↓␈αor␈α␈↓αfalse␈↓.␈α However,␈αthe␈αtruth␈αor␈αfalsity␈αof␈αa␈αformula␈αis␈αrelative␈αto␈αthe␈αinterpretation␈αassigned
␈↓ ↓H␈↓to␈α
the␈α
constants,␈α
the␈α∞function␈α
and␈α
predicate␈α
symbols␈α
and␈α∞the␈α
free␈α
variables␈α
of␈α
the␈α∞formula.␈α
We
␈↓ ↓H␈↓proceed as follows:
␈↓ ↓H␈↓ We␈αbegin␈αby␈αchoosing␈αa␈αdomain.␈α In␈αmost␈αcases,␈αthe␈αdomain␈αwill␈αinclude␈αthe␈αS-expressions,
␈↓ ↓H␈↓and␈αany␈αS-expression␈α
constants␈αappearing␈αin␈αthe␈α
formula␈αstand␈αfor␈α
themselves.␈α We␈αwill␈αallow␈α
for
␈↓ ↓H␈↓the␈α
possibility␈α
that␈αother␈α
objects␈α
than␈αS-expressions␈α
exist,␈α
and␈αsome␈α
constants␈α
may␈αdesignate␈α
them.
␈↓ ↓H␈↓The␈α
special␈α
constant␈α
symbol␈α
␈↓π|␈↓␈α
is␈α
used␈α
for␈α
the␈α
value␈α
of␈α
the␈α
function␈α
defined␈α
by␈α
a␈α
LISP␈αprogram␈α
in
␈↓ ↓H␈↓case the computation doesn't terminate; its use requires a justification that will be given later.
␈↓ ↓H␈↓ Each␈α⊃function␈α⊃or␈α⊃predicate␈α∩constant␈α⊃symbol␈α⊃is␈α⊃assigned␈α∩a␈α⊃function␈α⊃or␈α⊃predicate␈α∩on␈α⊃the
␈↓ ↓H␈↓domain␈α⊗ We␈α⊗will␈α⊗normally␈α∃assign␈α⊗to␈α⊗the␈α⊗basic␈α∃LISP␈α⊗function␈α⊗and␈α⊗predicate␈α⊗symbols␈α∃the
␈↓ ↓H␈↓corresponding␈α∞basic␈α∞LISP␈α∞functions␈α∞and␈α∞predicates.␈α∞ A␈α∞predicate␈α∞that␈α∞has␈α∞been␈α∞designated␈α∞as␈α∞a
␈↓ ↓H␈↓sort␈αis␈αassigned␈αthe␈αcharacteristic␈αpredicate␈αof␈αthe␈αsubdomain␈αconsisting␈αof␈αall␈αthe␈αelements␈αof␈αthat
␈↓ ↓H␈↓sort.␈α (The␈αcharacteristic␈αpredicate␈αof␈αa␈αset␈αis␈αthe␈αpredicate␈αthat␈αis␈α␈↓αtrue␈↓␈αexactly␈αon␈αelements␈αof␈αthe
␈↓ ↓H␈↓set).␈α⊂ We␈α∂require␈α⊂that␈α⊂the␈α∂subdomain␈α⊂corresponding␈α⊂to␈α∂a␈α⊂sort␈α⊂be␈α∂non-empty.␈α⊂ Thus␈α⊂␈↓↓isesexp␈↓␈α∂is
␈↓ ↓H␈↓assigned␈α⊂to␈α⊂the␈α⊂characteristic␈α⊂predicate␈α⊂of␈α⊂the␈α⊂subdomain␈α⊂of␈α⊂extended␈α⊂S-expressions,␈α⊂␈↓↓issexp␈↓␈α⊂is
␈↓ ↓H␈↓assigned␈αthe␈αcharacteristic␈αpredicate␈αof␈αthe␈αsubdomain␈αof␈αS-expressions,␈αand␈α␈↓↓islist␈↓␈αis␈αassigned␈αthe
␈↓ ↓H␈↓characteristic␈αpredicate␈αfor␈αthe␈αsubdomain␈αof␈αlists.␈α Function␈αand␈αpredicate␈αparameter␈αsymbols␈αdo
␈↓ ↓H␈↓not␈α∞get␈α∞assigned␈α∞any␈α∞meaning.␈α∞ Their␈α∞use␈α∂is␈α∞mainly␈α∞in␈α∞writing␈α∞axiom␈α∞schemata␈α∞from␈α∂which␈α∞an
␈↓ ↓H␈↓axiom␈α∀can␈α∀be␈α∀obtained␈α∀by␈α∀substituting␈α∀an␈α∀actual␈α∀function␈α∀or␈α∀predicate␈α∀expression␈α∃for␈α∀the
␈↓ ↓H␈↓parameter.
␈↓ ↓H␈↓ Each␈α
variable␈α
appearing␈αfree␈α
in␈α
a␈α
formula␈αis␈α
assigned␈α
an␈α
element␈αof␈α
the␈α
domain.␈α A␈α
general
␈↓ ↓H␈↓variable␈α∞maybe␈α∞assigned␈α∞any␈α∞element␈α∞of␈α∞the␈α∞domain.␈α∞ A␈α∞sorted␈α∞variable␈α∞is␈α∞assigned␈α∂an␈α∞element
␈↓ ↓H␈↓from␈α∞the␈α
corresponding␈α∞subdomain.␈α
This␈α∞collection␈α
of␈α∞assignments␈α
constitutes␈α∞an␈α
interpretation,
␈↓ ↓H␈↓and the truth of a formula is relative to the interpretation.
␈↓ ↓H␈↓ The␈α⊂truth␈α⊂of␈α⊂a␈α∂formula␈α⊂is␈α⊂determined␈α⊂from␈α⊂the␈α∂values␈α⊂of␈α⊂its␈α⊂constituents␈α⊂by␈α∂evaluating
␈↓ ↓H␈↓successively␈α∞larger␈α∞subexpressions.␈α
The␈α∞rules␈α∞for␈α
handling␈α∞functions␈α∞and␈α∞predicates,␈α
conditional
␈↓ ↓H␈↓expressions,␈αequality,␈αand␈αBoolean␈αexpressions␈αare␈αexactly␈αthe␈αsame␈αas␈αthose␈αwe␈αhave␈αused␈αin␈αthe
␈↓ ↓H␈↓previous chapters. We need only explain quantifiers and lambdas.
␈↓ ↓H␈↓ ␈↓↓[∀x␈↓β1␈↓↓ ... x␈↓βn␈↓↓: s]␈↓␈αis␈α␈↓αtrue␈↓␈αif␈αand␈αonly␈αif␈α␈↓↓s␈↓␈αis␈α␈↓αtrue␈↓␈αfor␈α␈↓↓all␈↓␈αallowed␈αassignments␈αof␈αelements␈αof␈αthe
␈↓ ↓H␈↓domain␈α
to␈α
the␈α␈↓↓x␈↓'s.␈α
An␈α
assignment␈α
is␈αallowed␈α
if␈α
the␈α
variable␈αis␈α
general␈α
or␈α
if␈αit␈α
is␈α
sorted␈α
and␈αthe
␈↓ ↓H␈↓assignment␈αis␈αto␈αan␈αelement␈αof␈αthe␈αcorresponding␈αsubdomain.␈α If␈α␈↓↓s␈↓␈αhas␈αfree␈αvariables␈αthat␈αare␈αnot
␈↓ ↓H␈↓among␈α
the␈α␈↓↓x␈↓'s,␈α
then␈αthe␈α
truth␈αof␈α
the␈α
formula␈αdepends␈α
on␈αthe␈α
values␈αassigned␈α
to␈α
these␈αremaining
␈↓ ↓H␈↓free␈α⊂variables.␈α⊂ ␈↓↓[∃x␈↓β1␈↓↓ ... x␈↓βn␈↓↓: s]␈↓␈α⊃is␈α⊂␈↓αtrue␈↓␈α⊂if␈α⊂and␈α⊃only␈α⊂if␈α⊂␈↓↓s␈↓␈α⊂is␈α⊃␈↓αtrue␈↓␈α⊂for␈α⊂␈↓↓some␈↓␈α⊂allowed␈α⊃assignment␈α⊂of
␈↓ ↓H␈↓elements of the domain to the ␈↓↓x␈↓'s. Free variables are handled just as before.
␈↓ ↓H␈↓ ␈↓↓[λx␈↓β1␈↓↓ ... x␈↓βn␈↓↓: e]␈↓␈α∂is␈α⊂assigned␈α∂a␈α∂function␈α⊂or␈α∂predicate␈α∂according␈α⊂to␈α∂whether␈α∂␈↓↓e␈↓␈α⊂is␈α∂a␈α∂term␈α⊂or␈α∂a
␈↓ ↓H␈↓formula.␈α The␈αvalue␈αof␈α␈↓↓[λx␈↓β1␈↓↓ ... x␈↓βn␈↓↓: e][t␈↓β1␈↓↓,...,t␈↓βn␈↓↓]␈↓␈αis␈αobtained␈αby␈αevaluating␈αthe␈α␈↓↓t␈↓'s␈αand␈αusing␈αthese
␈↓ ↓H␈↓values␈α
as␈α
values␈α
of␈α
the␈α
␈↓↓x␈↓'s␈α
in␈α
the␈α
evaluation␈α
of␈α␈↓↓e.␈↓␈α
If␈α
␈↓↓e␈↓␈α
has␈α
free␈α
variables␈α
in␈α
addition␈α
to␈α
the␈α␈↓↓x␈↓'s,␈α
the
␈↓ ↓H␈↓function or predicate assigned will depend on them too.
␈↓ ↓H␈↓ To␈αsee␈α
how␈αsorts␈αactually␈α
work,␈αwe␈αwill␈α
examine␈αsome␈α
typical␈αsentences.␈α Assume␈α
␈↓↓x␈↓␈αis␈αof␈α
sort
␈↓ ↓H␈↓54␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓␈↓↓issexp␈↓␈α
and␈α
␈↓↓XX␈↓␈α
is␈α∞a␈α
general␈α
variable.␈α
Then␈α∞the␈α
sentence␈α
␈↓↓∀x: [¬␈↓αat|␈↓↓x ⊃ x = ␈↓αa|␈↓↓x . ␈↓αd|␈↓↓x]␈↓␈α
has␈α∞the␈α
same
␈↓ ↓H␈↓truth␈α
value␈α
as␈α
␈↓↓∀XX: [issexp XX ⊃ [¬␈↓αat|␈↓↓XX ⊃ XX = ␈↓αa|␈↓↓XX . ␈↓αd|␈↓↓XX]]␈↓␈αand␈α
the␈α
sentencc␈α
␈↓↓∃x: ¬␈↓αat|␈↓↓x␈↓␈αhas
␈↓ ↓H␈↓the␈α∩same␈α∩truth␈α∩value␈α∩as␈α∩ ␈↓↓∃XX: [issexp XX ∧ ¬␈↓αat|␈↓↓XX]␈↓.␈α∩ This␈α∩ follows␈α∩from␈α∩the␈α∪rules␈α∩forming
␈↓ ↓H␈↓interpretations␈αand␈αevaluating␈αquantified␈αexpressions.␈α The␈αformula␈α␈↓↓∃x: issexp x␈↓␈αis␈αguaranteed␈αto
␈↓ ↓H␈↓be␈α␈↓αtrue␈↓␈αsince␈αwe␈αhave␈αrequired␈αthat␈αthe␈αsubdomain␈αcorresponding␈αto␈α␈↓↓issexp␈↓␈α(or␈αany␈αsort)␈αis␈αnon-
␈↓ ↓H␈↓empty.
␈↓ ↓H␈↓ Those␈α
who␈αare␈α
familiar␈αwith␈α
the␈α
lambda␈αcalculus␈α
should␈αnote␈α
that␈α
λ␈αis␈α
being␈αused␈α
here␈αin␈α
a
␈↓ ↓H␈↓very␈α
limited␈α
way.␈α Namely,␈α
the␈α
variables␈αin␈α
a␈α
lambda-expression␈α
take␈αonly␈α
elements␈α
of␈αthe␈α
domain
␈↓ ↓H␈↓as␈α⊂values,␈α⊂whereas␈α∂the␈α⊂essence␈α⊂of␈α∂the␈α⊂lambda␈α⊂calculus␈α∂is␈α⊂that␈α⊂they␈α∂take␈α⊂arbitrary␈α⊂functions␈α∂as
␈↓ ↓H␈↓values. We may call these restricted lambda expressions ␈↓↓first␈↓ ␈↓↓order␈↓ ␈↓↓lambdas.␈↓
␈↓ ↓H␈↓3. ␈↓αConditional Expressions.␈↓
␈↓ ↓H␈↓ All the properties we shall use of conditional forms follow from the relation
␈↓ ↓H␈↓3.1)␈↓ β>␈↓↓[s ⊃ [␈↓αif␈↓↓ s ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] = a] ∧ [¬s ⊃ [␈↓αif␈↓↓ s ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] = b] ␈↓.
␈↓ ↓H␈↓ It␈α
is␈α∞worthwhile␈α
to␈α
list␈α∞separately␈α
some␈α
properties␈α∞of␈α
conditional␈α
terms.␈α∞ First␈α
we␈α∞have␈α
the
␈↓ ↓H␈↓obvious
␈↓ ↓H␈↓␈↓ ¬␈↓↓[␈↓αif␈↓↓ T ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] = a ␈↓
␈↓ ↓H␈↓3.2)
␈↓ ↓H␈↓␈↓ ∧X␈↓↓[␈↓αif␈↓↓ F ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] = b . ␈↓
␈↓ ↓H␈↓Next we have a ␈↓↓distributive␈↓ ␈↓↓law␈↓ for functions applied to conditional terms, namely
␈↓ ↓H␈↓3.3)␈↓ ∧#␈↓↓f[␈↓αif␈↓↓ s ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] = ␈↓αif␈↓↓ s ␈↓αthen␈↓↓ f[a] ␈↓αelse␈↓↓ f[b] ␈↓.
␈↓ ↓H␈↓This␈α
applies␈αto␈α
predicates␈αas␈α
well␈αas␈α
functions␈αand␈α
can␈α
also␈αbe␈α
used␈αwhen␈α
one␈αof␈α
the␈αarguments␈α
of
␈↓ ↓H␈↓a␈αfunction␈αof␈αseveral␈αarguments␈αis␈αa␈αconditional␈αterm.␈α It␈αalso␈αapplies␈αwhen␈αone␈αof␈αthe␈αterms␈αof␈αa
␈↓ ↓H␈↓conditional term is itself a conditional term.
␈↓ ↓H␈↓Thus
␈↓ ↓H␈↓3.4)␈↓ β≤␈↓↓␈↓αif␈↓↓ [␈↓αif␈↓↓ r ␈↓αthen␈↓↓ s␈↓β1␈↓↓ ␈↓αelse␈↓↓ s␈↓β2␈↓↓] ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b = ␈↓
␈↓ ↓H␈↓␈↓ βl␈↓↓␈↓αif␈↓↓ r ␈↓αthen␈↓↓ [␈↓αif␈↓↓ s␈↓β1␈↓↓ ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] ␈↓αelse␈↓↓ [␈↓αif␈↓↓ s␈↓β2␈↓↓ ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ αn␈↓↓␈↓αif␈↓↓ r ␈↓αthen␈↓↓ [␈↓αif␈↓↓ s ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] ␈↓αelse␈↓↓ c = ␈↓
␈↓ ↓H␈↓3.5)␈↓ β ␈↓↓␈↓αif␈↓↓ s ␈↓αthen␈↓↓ [␈↓αif␈↓↓ r ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ c] ␈↓αelse␈↓↓ [␈↓αif␈↓↓ r ␈↓αthen␈↓↓ b ␈↓αelse␈↓↓ c] . ␈↓
␈↓ ↓H␈↓ When␈αthe␈αexpressions␈αfollowing␈α␈↓αthen␈↓␈αand␈α␈↓αelse␈↓␈αare␈αsentences,␈αthen␈αthe␈αconditional␈α
term␈αcan
␈↓ ↓H␈↓be replaced by a sentence according to
␈↓ ↓H␈↓3.6)␈↓ ∧1␈↓↓[␈↓αif␈↓↓ r ␈↓αthen␈↓↓ s␈↓β1␈↓↓ ␈↓αelse␈↓↓ s␈↓β2␈↓↓] ≡ [r ∧ s␈↓β1␈↓↓] ∨ [¬r ∧ s␈↓β2␈↓↓] ␈↓.
␈↓ ↓H␈↓These␈α
two␈α
rules␈α
permit␈αeliminating␈α
conditional␈α
terms␈α
from␈αsentences␈α
by␈α
first␈α
using␈αdistributivity␈α
to
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *55
␈↓ ↓H␈↓move␈α
the␈α
conditionals␈α
to␈α
the␈α
outside␈α
of␈α
any␈α
functions␈α
and␈α
then␈α
replacing␈α
the␈α
conditional␈α
term␈αby␈α
a
␈↓ ↓H␈↓Boolean expression.
␈↓ ↓H␈↓ Note␈αthat␈αthe␈αelimination␈αof␈α
conditional␈αterms␈αmay␈αincrease␈αthe␈α
size␈αof␈αa␈αsentence,␈αbecause␈α
␈↓↓r␈↓
␈↓ ↓H␈↓occurs␈αtwice␈αin␈αthe␈α
right␈αhand␈αside␈αof␈α
the␈αabove␈αequivalence.␈α In␈α
the␈αmost␈αunfavorable␈αcase,␈α
␈↓↓r␈↓␈αis
␈↓ ↓H␈↓dominates␈α∩the␈α⊃size␈α∩of␈α⊃the␈α∩expression␈α⊃so␈α∩that␈α⊃writing␈α∩it␈α⊃twice␈α∩almost␈α⊃doubles␈α∩the␈α⊃size␈α∩of␈α⊃the
␈↓ ↓H␈↓expression.
␈↓ ↓H␈↓ Suppose␈αthat␈α␈↓↓a␈↓␈αand␈α␈↓↓b␈↓␈αin␈α␈↓↓␈↓αif␈↓↓ s ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b␈↓␈αare␈αexpressions␈αthat␈αmay␈αcontain␈αthe␈αsentence␈α␈↓↓s.␈↓
␈↓ ↓H␈↓Occurrences␈αof␈α␈↓↓s␈↓␈α
in␈α␈↓↓a␈↓␈αcan␈αbe␈α
replaced␈αby␈αT,␈α
and␈αoccurrences␈αof␈α␈↓↓s␈↓␈α
in␈α␈↓↓b␈↓␈αcan␈α
be␈αreplaced␈αby␈αF.␈α
This
␈↓ ↓H␈↓follows from the fact that ␈↓↓a␈↓ is only evaluated if ␈↓↓s␈↓ is true and ␈↓↓b␈↓ is evaluated only if ␈↓↓s␈↓ is false.
␈↓ ↓H␈↓ This␈α∂leads␈α⊂to␈α∂a␈α⊂strengthened␈α∂form␈α⊂of␈α∂the␈α⊂law␈α∂of␈α⊂replacement␈α∂of␈α⊂equals␈α∂by␈α⊂equals.␈α∂ The
␈↓ ↓H␈↓ordinary␈αform␈αof␈αthe␈αlaw␈αsays␈αthat␈αif␈αwe␈αhave␈α␈↓↓e = e'␈↓,␈αthen␈αwe␈αcan␈αreplace␈αany␈αoccurrence␈αof␈α
␈↓↓e␈↓␈αin
␈↓ ↓H␈↓an␈α
expression␈α∞by␈α
an␈α
occurrence␈α∞of␈α
␈↓↓e'.␈↓␈α
However,␈α∞if␈α
we␈α
want␈α∞to␈α
replace␈α
␈↓↓e␈↓␈α∞by␈α
␈↓↓e'␈↓␈α
within␈α∞␈↓↓a␈↓␈α
within
␈↓ ↓H␈↓␈↓↓␈↓αif␈↓↓ s ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b␈↓,␈α∞then␈α∞we␈α∞need␈α∞only␈α∞prove␈α∞␈↓↓s ⊃ e =e'␈↓,␈α∞and␈α∞to␈α∞make␈α∞the␈α∞replacement␈α∞within␈α∞␈↓↓b␈↓␈α
we
␈↓ ↓H␈↓need only prove ␈↓↓¬s ⊃ e = e'␈↓.
␈↓ ↓H␈↓ More␈αfacts␈αabout␈αconditional␈αterms␈αare␈αgiven␈αin␈α[McCarthy␈α1963]␈αincluding␈αa␈αdiscussion␈αof
␈↓ ↓H␈↓canonical␈αforms␈αthat␈αparallels␈α
the␈αcanonical␈αforms␈αof␈α
Boolean␈αterms.␈α Any␈αquestion␈αof␈α
equivalence
␈↓ ↓H␈↓of␈α
conditional␈α∞terms␈α
is␈α
decidable␈α∞by␈α
truth␈α∞tables␈α
analogously␈α
to␈α∞the␈α
decidability␈α∞of␈α
propositional
␈↓ ↓H␈↓sentences.
␈↓ ↓H␈↓4. ␈↓αLambda-expressions.␈↓
␈↓ ↓H␈↓ The␈α∪only␈α∩rule␈α∪required␈α∩for␈α∪handling␈α∪lambda-expressions␈α∩in␈α∪first␈α∩order␈α∪logic␈α∪is␈α∩called
␈↓ ↓H␈↓␈↓↓lambda-conversion.␈↓ Essentially it is
␈↓ ↓H␈↓␈↓ βt␈↓↓[λx: e][a] =␈↓ < the result of substituting ␈↓↓a␈↓ for ␈↓↓x␈↓ in ␈↓↓e␈↓ >.
␈↓ ↓H␈↓As examples of this rule, we have
␈↓ ↓H␈↓␈↓ ∧+␈↓↓[λx: ␈↓αa|␈↓↓x . y][u . v] = [␈↓αa|␈↓↓[u . v]] . y . ␈↓
␈↓ ↓H␈↓However,␈αa␈α
complication␈αrequires␈α
modifying␈αthe␈α
rule.␈α Namely,␈α
we␈αcan't␈α
substitute␈αfor␈α
a␈αvariable␈α
␈↓↓x␈↓
␈↓ ↓H␈↓an␈αexpression␈α␈↓↓e␈↓␈αthat␈αhas␈αa␈αfree␈αvariable␈α␈↓↓y␈↓␈αinto␈αa␈αcontext␈αin␈αwhich␈α␈↓↓y␈↓␈αis␈αbound.␈α Thus␈αit␈αwould␈αbe
␈↓ ↓H␈↓wrong␈αto␈αsubstitute␈α␈↓↓y + z␈↓␈αfor␈α␈↓↓x␈↓␈αin␈α␈↓↓∀y: [x + y = z]␈↓␈αor␈αinto␈αthe␈αterm␈α␈↓↓[λy: x + y][u + v]␈↓.␈α Before␈αdoing
␈↓ ↓H␈↓the␈αsubstitution,␈αthe␈αvariable␈α␈↓↓y␈↓␈αwould␈αhave␈αto␈αbe␈αreplaced␈αin␈αall␈αits␈αbound␈αoccurrences␈αby␈αa␈αfresh
␈↓ ↓H␈↓variable.
␈↓ ↓H␈↓ Lambda-expressions␈α∀can␈α∀always␈α∃be␈α∀eliminated␈α∀from␈α∃sentences␈α∀and␈α∀terms␈α∃by␈α∀lambda-
␈↓ ↓H␈↓conversion,␈αbut␈αthe␈αexpression␈αmay␈α
increase␈αgreatly␈αin␈αlength␈αif␈α
a␈αlengthy␈αterm␈αreplaces␈αa␈α
variable
␈↓ ↓H␈↓that␈α∞occurs␈α∞more␈α∞than␈α∞once␈α∞in␈α
␈↓↓e.␈↓␈α∞ It␈α∞is␈α∞easy␈α∞to␈α∞make␈α
an␈α∞expression␈α∞of␈α∞length␈α∞proportional␈α∞to␈α
␈↓↓n␈↓
␈↓ ↓H␈↓whose␈α⊃length␈α⊃is␈α⊃proportional␈α∩to␈α⊃2␈↓∧n␈↓␈α⊃after␈α⊃conversion␈α∩of␈α⊃its␈α⊃␈↓↓n␈↓␈α⊃nested␈α∩lambda-expressions.␈α⊃ For
␈↓ ↓H␈↓example
␈↓ ↓H␈↓ ␈↓ ∧T␈↓↓λx␈↓β1␈↓↓: [x␈↓β1␈↓↓.x␈↓β1␈↓↓][ ... [λx␈↓βn␈↓↓: [x␈↓βn␈↓↓.x␈↓βn␈↓↓][␈↓¬A␈↓↓]] ... ]␈↓
␈↓ ↓H␈↓56␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓becomes
␈↓ ↓H␈↓ ␈↓¬(A . A)␈↓,
␈↓ ↓H␈↓ ␈↓¬((A . A) . (A . A))␈↓,
␈↓ ↓H␈↓or
␈↓ ↓H␈↓ ␈↓¬((((A . A) . (A . A)) . ((A . A) . (A . A))) . ((A . A) . (A . A)) . ((A . A) . (A . A))))␈↓
␈↓ ↓H␈↓for ␈↓↓n␈↓ = 1, 2, or 4 respectively.
␈↓ ↓H␈↓5. ␈↓αFirst Order Axioms for LISP.␈↓
␈↓ ↓H␈↓ We␈α∪are␈α∪interested␈α∩in␈α∪the␈α∪first␈α∪order␈α∩theory␈α∪of␈α∪LISP.␈α∪ That␈α∩is,␈α∪we␈α∪are␈α∪interested␈α∩ in
␈↓ ↓H␈↓determining␈αwhat␈αformulas␈αinvolving␈αvariables␈αranging␈αover␈αlists␈αand␈αS-expressions␈αand␈αfunction
␈↓ ↓H␈↓and␈αpredicate␈αsymbols␈αrepresenting␈α
LISP␈αfunctions␈αand␈αpredicates␈α
are␈α␈↓αtrue␈↓.␈α In␈αorder␈α
to␈αdescribe
␈↓ ↓H␈↓this␈α∞theory␈α∞we␈α∞represent␈α∞the␈α∞algebraic␈α∂facts␈α∞about␈α∞lists,␈α∞S-expressions␈α∞and␈α∞basic␈α∂LISP␈α∞functions
␈↓ ↓H␈↓and␈αpredicates␈αby␈αaxioms␈α(sentences␈αof␈αthe␈αtheory␈αwhich␈αwe␈αassume␈αto␈αbe␈α␈↓αtrue␈↓).␈α The␈αaxioms␈αare
␈↓ ↓H␈↓given␈α∂names␈α∂either␈α∂individually␈α∂or␈α∂in␈α∂small␈α∂groups.␈α∂To␈α∂refer␈α∂to␈α∂the␈α∂second␈α∂axiom␈α∂in␈α⊂a␈α∂group
␈↓ ↓H␈↓named ␈↓¬AXNAME ␈↓we write ␈↓¬AXNAME2. ␈↓
␈↓ ↓H␈↓αSorts and variables.
␈↓ ↓H␈↓ Before␈α
stating␈α
the␈α
axioms␈α∞we␈α
list␈α
the␈α
sorts␈α
and␈α∞give␈α
the␈α
variables␈α
which␈α
range␈α∞over␈α
these
␈↓ ↓H␈↓sorts.␈α∩ The␈α∩statement␈α∩␈↓↓x ε issexp␈↓␈α∪means␈α∩that␈α∩the␈α∩variable␈α∪␈↓↓x␈↓␈α∩ranges␈α∩over␈α∩the␈α∪subdomain␈α∩with
␈↓ ↓H␈↓characteristic predicate ␈↓↓issexp.␈↓
␈↓ ↓H␈↓␈↓¬SORTS: ␈↓ ␈↓↓isesexp,␈↓ ␈↓↓issexp,␈↓ ␈↓↓islist␈↓
␈↓ ↓H␈↓␈↓¬VARIABLES: ␈↓
␈↓ ↓H␈↓ ␈↓¬GENERAL: ␈↓␈↓ ∧8␈↓↓XX, YY, ZZ␈↓
␈↓ ↓H␈↓ ␈↓¬SORTED: ␈↓␈↓ ∧8␈↓↓X, Y, Z␈↓ ε ␈↓↓isesexp␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓x, y, z␈↓ ε ␈↓↓issexp␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓u, v, w␈↓ ε ␈↓↓islist␈↓
␈↓ ↓H␈↓ Note␈α
that␈α
from␈α
these␈α
declarations␈α
we␈α
can␈α
show␈α
such␈α
facts␈α
as␈α
␈↓↓∀x: issexp x␈↓.␈α
To␈α
see␈α∞this,␈α
we
␈↓ ↓H␈↓observe␈α∞that␈α
this␈α∞sentence␈α∞has␈α
the␈α∞same␈α
truth␈α∞value␈α∞as␈α
␈↓↓∀X: issexp X ⊃ issexp X␈↓␈α∞which␈α∞clearly␈α
is
␈↓ ↓H␈↓␈↓αtrue␈↓.
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *57
␈↓ ↓H␈↓αDomain facts.
␈↓ ↓H␈↓ The␈α∀first␈α∀group␈α∀of␈α∀axioms␈α∀give␈α∀facts␈α∀about␈α∀the␈α∀various␈α∀sorts(subdomains)␈α∃and␈α∀their
␈↓ ↓H␈↓relations.
␈↓ ↓H␈↓␈↓¬ESEXP: ␈↓ ␈↓ β8␈↓↓∀x: isesexp x␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓isesexp ␈↓π|␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓∀X: issexp X ∨ X = ␈↓π|␈↓↓␈↓
␈↓ ↓H␈↓␈↓¬SEXP: ␈↓ ␈↓ β8␈↓↓∀u: issexp u␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓¬issexp ␈↓π|␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓∀XX: ␈↓αat|␈↓↓XX ⊃ issexp XX␈↓
␈↓ ↓H␈↓␈↓¬NIL: ␈↓ ␈↓ β8␈↓↓islist ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓¬NULL: ␈↓ ␈↓ β8␈↓↓∀u: [␈↓αn|␈↓↓u ≡ u=␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓∀u: [␈↓αn|␈↓↓u ≡ ␈↓αat|␈↓↓u]␈↓
␈↓ ↓H␈↓αFunction ranges.
␈↓ ↓H␈↓ Next are the axioms describing the ranges of the basic LISP functions.
␈↓ ↓H␈↓␈↓¬CONS: ␈↓ ␈↓ β8␈↓↓∀x y: ¬␈↓αat|␈↓↓[x . y]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓∀x y: issexp [x . y]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓∀x u: islist [x . u]␈↓
␈↓ ↓H␈↓␈↓¬CAR: ␈↓ ␈↓ β8␈↓↓∀x: [¬␈↓αat|␈↓↓x ⊃ issexp ␈↓αa|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓¬CDR: ␈↓ ␈↓ β8␈↓↓∀x: [¬␈↓αat|␈↓↓x ⊃ issexp ␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓∀u: [¬␈↓αn|␈↓↓u ⊃ islist ␈↓αd|␈↓↓u]␈↓
␈↓ ↓H␈↓αFunction relations.
␈↓ ↓H␈↓ Finally we have the axioms describing the relations among the basic LISP functions.
␈↓ ↓H␈↓␈↓¬CAR-CONS: ␈↓ ␈↓ β8␈↓↓∀x y: ␈↓αa|␈↓↓[x . y] = x␈↓
␈↓ ↓H␈↓␈↓¬CDR-CONS: ␈↓ ␈↓ β8␈↓↓∀x y: ␈↓αd|␈↓↓[x . y] = y␈↓
␈↓ ↓H␈↓␈↓¬CONSTRUCT: ␈↓ ␈↓ β8␈↓↓∀x: [¬␈↓αat|␈↓↓x ⊃ [␈↓αa|␈↓↓x . ␈↓αd|␈↓↓x] = x]␈↓
␈↓ ↓H␈↓58␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓ An alternate form of ␈↓¬CONSTRUCT ␈↓which we will find useful is
␈↓ ↓H␈↓␈↓¬EQ-SEXP: ␈↓ ␈↓ β8␈↓↓∀x y: [[¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y] ⊃ [␈↓αa|␈↓↓x = ␈↓αa|␈↓↓y ∧ ␈↓αd|␈↓↓x = ␈↓αd|␈↓↓y ≡ x = y]] ␈↓.
␈↓ ↓H␈↓ These␈α
axioms␈α
are␈α
analogous␈α
to␈α
the␈αalgebraic␈α
part␈α
of␈α
Peano's␈α
axioms␈α
for␈α
the␈αnon-negative
␈↓ ↓H␈↓integers.␈α The␈αanalogy␈αcan␈αbe␈αmade␈αclear␈αif␈αwe␈αwrite␈αPeano's␈αaxioms␈αusing␈α␈↓↓n'␈↓␈αfor␈αthe␈αsuccessor␈α
of
␈↓ ↓H␈↓␈↓↓n␈↓ and ␈↓↓n␈↓␈↓∧-␈↓ for the predecessor of ␈↓↓n.␈↓ Peano's algebraic axioms then become
␈↓ ↓H␈↓␈↓ ∧x␈↓↓∀n: n' ≠ 0 , ␈↓
␈↓ ↓H␈↓␈↓ ∧|␈↓↓∀n: (n')␈↓∧-␈↓↓ = n , ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ∧x␈↓↓∀n: n ≠ 0 ⊃ (n␈↓∧-␈↓↓)' = n . ␈↓
␈↓ ↓H␈↓Lists␈αcan␈α
model␈αintegers␈α
if␈αwe␈α
identify␈α0␈α
with␈α␈↓¬NIL␈↓␈α
and␈αassume␈α
that␈αthere␈α
is␈αonly␈α
one␈αobject␈α(say␈α
␈↓¬NIL␈↓
␈↓ ↓H␈↓again) that can serve as a list element. Then ␈↓↓n' = cons[␈↓¬NIL␈↓↓,n]␈↓, and ␈↓↓n␈↓∧-␈↓↓ = ␈↓αd|␈↓↓n␈↓.
␈↓ ↓H␈↓6. ␈↓αAxiom Schemas of Induction.␈↓
␈↓ ↓H␈↓ Clearly␈α∞S-expressions␈α
and␈α∞lists␈α
satisfy␈α∞the␈α∞axioms␈α
given␈α∞for␈α
them,␈α∞but␈α∞unfortunately␈α
these
␈↓ ↓H␈↓algebraic␈α∞axioms␈α∞are␈α∞insufficient␈α∞to␈α∞characterize␈α∂them.␈α∞ For␈α∞example,␈α∞consider␈α∞a␈α∞domain␈α∂of␈α∞one
␈↓ ↓H␈↓element ␈↓↓a␈↓ satisfying
␈↓ ↓H␈↓␈↓ ¬↓␈↓↓␈↓αa|␈↓↓a = ␈↓αd|␈↓↓a = a . a = a . ␈↓
␈↓ ↓H␈↓It␈αsatisfies␈αthe␈αalgebraic␈αaxioms␈αfor␈αS-expressions.␈α We␈αcan␈αexclude␈αit␈αby␈αan␈αaxiom␈α␈↓↓∀x:␈α[␈↓αa|␈↓↓x␈α≠␈αx]␈↓,
␈↓ ↓H␈↓but␈αthis␈αwon't␈α
exclude␈αother␈αcircular␈αlist␈α
structures␈αthat␈αeventually␈αreturn␈α
to␈αthe␈αsame␈α
element␈αby
␈↓ ↓H␈↓some␈α∞␈↓αa␈↓-␈↓αd␈↓␈α∂chain.␈α∞ Actually␈α∂we␈α∞want␈α∂to␈α∞exclude␈α∞all␈α∂infinite␈α∞chains,␈α∂because␈α∞most␈α∂LISP␈α∞programs
␈↓ ↓H␈↓won't␈αterminate␈αunless␈α
every␈α␈↓αa␈↓-␈↓αd␈↓␈αchain␈α
eventually␈αterminates␈αin␈α
an␈αatom.␈α This␈α
cannot␈αbe␈αdone␈α
by
␈↓ ↓H␈↓any finite set of axioms.
␈↓ ↓H␈↓ In␈α∂order␈α∂to␈α∂exclude␈α⊂circular␈α∂and␈α∂infinite␈α∂list␈α⊂structures␈α∂we␈α∂need␈α∂axiom␈α⊂schemas␈α∂of
␈↓ ↓H␈↓induction analogous to Peano's for the integers. Peano's induction schema is ordinarily written
␈↓ ↓H␈↓␈↓ ∧S␈↓↓P(0) ∧ ∀n: (P(n) ⊃ P(n')) ⊃ ∀n: P(n) .␈↓
␈↓ ↓H␈↓Here␈α␈↓↓P(n)␈↓␈αis␈αan␈αarbitrary␈α
predicate␈αof␈αintegers,␈αand␈αwe␈α
get␈αparticular␈αinstances␈αof␈αthe␈α
schema␈αby
␈↓ ↓H␈↓substituting␈αparticular␈αpredicates.␈α It␈αis␈αcalled␈αan␈αaxiom␈αschema␈αrather␈αthan␈αan␈αaxiom,␈αbecause␈αwe
␈↓ ↓H␈↓consider␈α∂the␈α∂schema,␈α∂which␈α∂is␈α∂not␈α∂properly␈α⊂a␈α∂sentence␈α∂of␈α∂first␈α∂order␈α∂logic,␈α∂as␈α∂standing␈α⊂for␈α∂the
␈↓ ↓H␈↓infinite␈α⊂collection␈α⊂of␈α∂axioms␈α⊂that␈α⊂arise␈α⊂from␈α∂it␈α⊂by␈α⊂substituting␈α⊂all␈α∂possible␈α⊂predicates␈α⊂for␈α⊂␈↓↓P.␈α∂ ␈↓
␈↓ ↓H␈↓The S-expression induction schema is
␈↓ ↓H␈↓␈↓¬SEXPINDUCTION: ␈↓ ␈↓↓∀x: [␈↓αat|␈↓↓x ⊃ ␈↓πF␈↓↓ x] ∧ ∀x: [¬␈↓αat|␈↓↓x ∧ ␈↓πF␈↓↓ ␈↓αa|␈↓↓x ∧ ␈↓πF␈↓↓ ␈↓αd|␈↓↓x ⊃ ␈↓πF␈↓↓ x] ⊃ ∀x: ␈↓πF␈↓↓ x ␈↓.
␈↓ ↓H␈↓ The corresponding axiom schema for lists is
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *59
␈↓ ↓H␈↓␈↓¬LISTINDUCTION: ␈↓ ␈↓↓∀u: [␈↓αn|␈↓↓u ⊃ ␈↓πF␈↓↓ u] ∧ ∀u: [¬␈↓αn|␈↓↓u ∧ ␈↓πF␈↓↓ ␈↓αd|␈↓↓u ⊃ ␈↓πF␈↓↓ u] ⊃ ∀u: ␈↓πF␈↓↓ u ␈↓.
␈↓ ↓H␈↓Here ␈↓πF␈↓ is a predicate variable.
␈↓ ↓H␈↓ These␈α∞schemas␈α∞are␈α
called␈α∞principles␈α∞of␈α
␈↓↓structural␈↓␈α∞␈↓↓induction,␈↓␈α∞since␈α
the␈α∞induction␈α∞is␈α∞on␈α
the
␈↓ ↓H␈↓structure␈α
of␈α
the␈α
entities␈α
involved.␈α
Other␈α
schemas␈α
derived␈α
from␈α
principles␈α
of␈α∞␈↓↓structural␈↓␈α
␈↓↓induction␈↓
␈↓ ↓H␈↓are␈αpossible␈αand␈αthe␈αbest␈αschema␈αto␈αuse␈αdepends␈αstongly␈αon␈αthe␈αproblem␈αat␈αhand.␈α Some␈αexamples
␈↓ ↓H␈↓will be given in a later section.
␈↓ ↓H␈↓ Even␈αthe␈αaxiom␈αschemas␈αdon't␈αassure␈αus␈αthat␈αthe␈αonly␈αdomain␈αsatisfying␈αthe␈αaxioms␈αis␈αthat
␈↓ ↓H␈↓of␈αthe␈αintegers␈αor␈αthe␈αS-expressions␈αas␈αthe␈α
case␈αmay␈αbe.␈α Any␈αfirst␈αorder␈αtheory␈αwhose␈αaxioms␈α
can
␈↓ ↓H␈↓be␈αgiven␈αeffectively␈α
admits␈αthe␈αso-called␈α␈↓↓non-standard models.␈↓␈α
However,␈αit␈αseems␈αlikely␈α
that␈αthe
␈↓ ↓H␈↓non-standard␈α
models␈α
of␈α
S-expressions,␈α
like␈α
the␈α
non-standard␈α
models␈α
of␈α
integers,␈α
will␈α
agree␈αwith
␈↓ ↓H␈↓the standard model for sentences of practical interest.
␈↓ ↓H␈↓7. ␈↓αA Simple Example. ␈↓
␈↓ ↓H␈↓ In␈αorder␈αto␈αcomplete␈αour␈αdescription␈αof␈αthe␈αfirst␈α
order␈αtheory␈αof␈αLISP␈αwe␈αneed␈αto␈αbe␈αable␈α
to
␈↓ ↓H␈↓represent␈α∞facts␈α∞about␈α
recursively␈α∞defined␈α∞LISP␈α∞functions.␈α
Given␈α∞a␈α∞recursive␈α∞function␈α
definition
␈↓ ↓H␈↓we␈α
can␈α
form␈α
the␈α
␈↓↓functional equation␈↓␈α
by␈α
replacing␈α
the␈α
"←"␈α
in␈α
the␈α
recursive␈α
function␈α
definition␈αby
␈↓ ↓H␈↓"="␈α⊂and␈α⊂universally␈α⊂quantifying␈α∂over␈α⊂the␈α⊂argument␈α⊂variables.␈α⊂In␈α∂the␈α⊂case␈α⊂of␈α⊂␈↓↓append␈↓␈α⊂which␈α∂is
␈↓ ↓H␈↓defined by
␈↓ ↓H␈↓␈↓ ∧F␈↓↓u * v ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]␈↓
␈↓ ↓H␈↓the corresponding functional equation is
␈↓ ↓H␈↓␈↓¬APPEND: ␈↓ ␈↓↓∀u v: [u * v = ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]] ␈↓.
␈↓ ↓H␈↓Sometimes␈αthe␈αfunctional␈αequation␈α characterizes␈αthe␈αfunction␈αcompletely.␈α For␈αthis␈αto␈αbe␈αthe␈αcase
␈↓ ↓H␈↓we␈α∩need␈α⊃to␈α∩know␈α∩that␈α⊃a␈α∩function␈α∩satisfying␈α⊃the␈α∩equation␈α⊃exists␈α∩and␈α∩that␈α⊃it␈α∩is␈α∩unique.␈α⊃This
␈↓ ↓H␈↓essentially␈αreduces␈αto␈αshowing␈αthat␈αthe␈αfunction␈αis␈αtotal.␈α If␈αthe␈αfunction␈αis␈αnot␈αtotal␈αthen␈αwe␈αneed
␈↓ ↓H␈↓the␈α∃additional␈α∃information␈α∃provided␈α∃by␈α∀the␈α∃␈↓↓minimiztion schema.␈↓␈α∃ This␈α∃is␈α∃justified␈α∃by␈α∀the
␈↓ ↓H␈↓semantics␈αof␈αLISP␈αprograms␈αbased␈αon␈αthe␈αuse␈αof␈αfunctionals␈αand␈αfixedpoints.␈α Before␈αproceeding
␈↓ ↓H␈↓with a more detailed discussion of these matters an example is in order.
␈↓ ↓H␈↓ For␈αthe␈αremainder␈αof␈αthis␈αsection␈αwe␈αwill␈αshow␈αhow␈αto␈αuse␈αthe␈αaxiom␈α␈↓¬APPEND␈α␈↓together␈αwith
␈↓ ↓H␈↓the LISP axioms to prove some properties of ␈↓↓append.␈↓ In particular we will show
␈↓ ↓H␈↓␈↓¬NIL-APPEND: ␈↓ ␈↓↓∀v: ␈↓¬NIL␈↓↓ * v = v␈↓
␈↓ ↓H␈↓␈↓¬ISTOT-APPEND: ␈↓ ␈↓↓∀u v: islist [u * v]␈↓
␈↓ ↓H␈↓␈↓¬CAR-APPEND: ␈↓ ␈↓↓∀u v: [¬␈↓αn|␈↓↓u ⊃ ␈↓αa|␈↓↓[u * v] = ␈↓αa|␈↓↓u]␈↓
␈↓ ↓H␈↓␈↓¬CDR-APPEND: ␈↓ ␈↓↓∀u v: [¬␈↓αn|␈↓↓u ⊃ ␈↓αd|␈↓↓[u * v] = [␈↓αd|␈↓↓u] * v]␈↓
␈↓ ↓H␈↓␈↓¬NOTNUL-APPEND: ␈↓ ␈↓↓∀u v: [[¬␈↓αn|␈↓↓u ∨ ¬␈↓αn|␈↓↓v] ⊃ ¬␈↓αn|␈↓↓[u * v]]␈↓
␈↓ ↓H␈↓60␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓␈↓¬AASOC-APPEND: ␈↓ ␈↓↓∀u v w: [u * v] * w = u * [v * w]␈↓
␈↓ ↓H␈↓ First we prove ␈↓¬NIL-APPEND. ␈↓ By ␈↓¬APPEND ␈↓and the axiom ␈↓¬NIL ␈↓we have
␈↓ ↓H␈↓␈↓ ∧3␈↓↓␈↓¬NIL␈↓↓ * v = ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓¬NIL␈↓↓ ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]␈↓
␈↓ ↓H␈↓and by ␈↓¬NULL ␈↓this simplifies to ␈↓↓␈↓¬NIL␈↓↓ * v = v␈↓ as desired.
␈↓ ↓H␈↓ Next we prove ␈↓¬ISTOT-APPEND, ␈↓i.e. that ␈↓↓append␈↓ is total. For this we use ␈↓¬LISTINDUCTION ␈↓with
␈↓ ↓H␈↓␈↓ ∧|␈↓↓␈↓πF␈↓↓ u ≡ ∀v: islist [u * v] ␈↓
␈↓ ↓H␈↓obtaining the induction axiom
␈↓ ↓H␈↓␈↓ βW␈↓↓[∀u: [␈↓αn|␈↓↓u ⊃ ∀v: islist [u * v]] ∧ ␈↓
␈↓ ↓H␈↓␈↓ β⎇␈↓↓∀u: [[¬␈↓αn|␈↓↓u ∧ ∀v: islist [␈↓αd|␈↓↓u * v]] ⊃ ∀v: islist [u * v]]]␈↓
␈↓ ↓H␈↓␈↓ ∧*␈↓↓⊃ ∀u v: islist [u * v] . ␈↓
␈↓ ↓H␈↓The␈αproof␈αthen␈α
breaks␈αinto␈αtwo␈αsteps␈α
corresponding␈αto␈αthe␈α
cases␈α␈↓↓␈↓αn|␈↓↓u␈↓␈αand␈α␈↓↓¬␈↓αn|␈↓↓u␈↓.␈α
In␈αthe␈αfirst␈αcase␈α
we
␈↓ ↓H␈↓must show
␈↓ ↓H␈↓␈↓ ∧{␈↓↓∀u: [␈↓αn|␈↓↓u ⊃ ∀v: islist [u * v]] ␈↓
␈↓ ↓H␈↓which follows directly from ␈↓¬APPEND ␈↓and ␈↓¬SORTED. ␈↓ In the second case we must show
␈↓ ↓H␈↓␈↓ βz␈↓↓∀u: [[¬␈↓αn|␈↓↓u ∧ ∀v: islist [␈↓αd|␈↓↓u * v]] ⊃ ∀v: islist [u * v]] ␈↓.
␈↓ ↓H␈↓To do this we assume
␈↓ ↓H␈↓␈↓ ∧}␈↓↓¬␈↓αn|␈↓↓u ∧ ∀v: islist [␈↓αd|␈↓↓u * v] . ␈↓
␈↓ ↓H␈↓Then by ␈↓¬NULL, ␈↓␈↓¬CAR ␈↓we have ␈↓↓issexp ␈↓αa|␈↓↓u␈↓ and so by ␈↓¬CONS3 ␈↓we have
␈↓ ↓H␈↓␈↓ ¬ ␈↓↓islist [␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]] . ␈↓
␈↓ ↓H␈↓From this and ␈↓¬APPEND, ␈↓since we are in the case ␈↓↓¬␈↓αn|␈↓↓u␈↓, it follows that
␈↓ ↓H␈↓␈↓ ¬,␈↓↓islist [u * v] . ␈↓
␈↓ ↓H␈↓␈↓¬ISTOT-APPEND ␈↓follows immediately from these two steps and the induction axiom.
␈↓ ↓H␈↓ We now prove ␈↓¬CAR-APPEND. ␈↓ From ␈↓¬APPEND ␈↓we have
␈↓ ↓H␈↓␈↓ ∧v␈↓↓¬␈↓αn|␈↓↓u ⊃ [u * v = ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]] .␈↓
␈↓ ↓H␈↓Now we use ␈↓¬CAR-CONS ␈↓and obtain
␈↓ ↓H␈↓␈↓ ¬α␈↓↓¬␈↓αn|␈↓↓u ⊃ [␈↓αa|␈↓↓[u * v] = ␈↓αa|␈↓↓u] ␈↓
␈↓ ↓H␈↓To␈αjustify␈αusing␈α␈↓¬CAR-CONS␈α␈↓we␈αneed␈α␈↓↓issexp␈α␈↓αa|␈↓↓u␈↓,␈α␈↓↓islist␈α␈↓αd|␈↓↓u␈↓␈αand␈α␈↓↓islist␈α[␈↓αd|␈↓↓u␈α*␈αv]␈↓␈αunder␈αthe␈αassumption
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *61
␈↓ ↓H␈↓that␈α␈↓↓¬␈↓αn␈↓↓ u␈↓.␈αThese␈α
follow␈αfrom␈α(␈↓¬NULL,␈α␈↓␈↓¬CAR),␈α
␈↓␈↓¬CDR2;␈α␈↓and␈α␈↓¬ISTOT-APPEND␈α␈↓respectively.␈α
␈↓¬CDR-APPEND␈α␈↓is
␈↓ ↓H␈↓proved similiarly with ␈↓¬CDR-CONS ␈↓in place of ␈↓¬CAR-CONS. ␈↓
␈↓ ↓H␈↓ To prove ␈↓¬NOTNUL-APPEND ␈↓we note first that
␈↓ ↓H␈↓␈↓ ∧⎇␈↓↓¬␈↓αn|␈↓↓u ⊃ ¬␈↓αn|␈↓↓[u * v] ␈↓
␈↓ ↓H␈↓follows from ␈↓¬APPEND, ␈↓␈↓¬NULL, ␈↓␈↓¬CAR, ␈↓␈↓¬CDR2, ␈↓␈↓¬ISTOT-APPEND, ␈↓and ␈↓¬CONS1. ␈↓ Second we have
␈↓ ↓H␈↓␈↓ ∧{␈↓↓[␈↓αn|␈↓↓u ∧ ¬␈↓αn|␈↓↓v] ⊃ ¬␈↓αn|␈↓↓[u * v] ␈↓
␈↓ ↓H␈↓by ␈↓¬APPEND. ␈↓ Combining these two we have the desired result.
␈↓ ↓H␈↓ Finally we prove ␈↓¬ASSOC-APPEND. ␈↓ Again we use ␈↓¬LISTINDUCTION, ␈↓this time with
␈↓ ↓H␈↓␈↓ ∧M␈↓↓␈↓πF␈↓↓ u ≡ ∀v w: [u * v] * w = u * [v * w] ␈↓.
␈↓ ↓H␈↓First we show
␈↓ ↓H␈↓␈↓ ∧.␈↓↓∀u: [␈↓αn|␈↓↓u ⊃ ∀v w: [u * v] * w = u * [v * w]] ␈↓.
␈↓ ↓H␈↓Assuming ␈↓↓␈↓αn|␈↓↓u␈↓ we have by ␈↓¬APPEND ␈↓that
␈↓ ↓H␈↓␈↓ ∧p␈↓↓[u * v] * w = v * w ␈↓
␈↓ ↓H␈↓and by ␈↓¬APPEND ␈↓and ␈↓¬ISTOT-APPEND ␈↓that
␈↓ ↓H␈↓␈↓ ∧p␈↓↓u * [v * w] = v * w ␈↓
␈↓ ↓H␈↓proving the first step.
␈↓ ↓H␈↓The second step is to show that
␈↓ ↓H␈↓␈↓ α!␈↓↓∀u: [[¬␈↓αn|␈↓↓u ∧ ∀v w: [␈↓αd|␈↓↓u * v] * w = [␈↓αd|␈↓↓u] * [v * w]] ⊃ ∀v w: [u * v] * w = u * [v * w]] ␈↓.
␈↓ ↓H␈↓We␈α
do␈α∞this␈α
by␈α∞assuming␈α
the␈α∞lefthand␈α
side␈α
of␈α∞the␈α
implication␈α∞and␈α
proving␈α∞a␈α
chain␈α∞of␈α
equalities
␈↓ ↓H␈↓which␈αwill␈αprove␈αthe␈αrighthand␈αside␈αof␈αthe␈αimplication.␈α By␈α␈↓¬APPEND,␈α␈↓␈↓¬NOTNUL-APPEND␈α␈↓and␈α␈↓¬ISTOT-
␈↓ ↓H␈↓¬APPEND ␈↓we have
␈↓ ↓H␈↓␈↓ ∧K␈↓↓[u * v] * w = ␈↓αa|␈↓↓[u * v] . [␈↓αd|␈↓↓[u * v] * w]␈↓
␈↓ ↓H␈↓and by ␈↓¬CAR-APPEND ␈↓and ␈↓¬CDR-APPEND ␈↓
␈↓ ↓H␈↓␈↓ ∧␈↓↓␈↓αa|␈↓↓[u * v] . [␈↓αd|␈↓↓[u * v] * w] = ␈↓αa|␈↓↓u . [[␈↓αd|␈↓↓u * v] * w] ␈↓.
␈↓ ↓H␈↓By assumption
␈↓ ↓H␈↓␈↓ ∧&␈↓↓␈↓αa|␈↓↓u . [[␈↓αd|␈↓↓u * v] * w] = ␈↓αa|␈↓↓u . [[␈↓αd|␈↓↓u] * [v * w]] ␈↓.
␈↓ ↓H␈↓Finally by ␈↓¬APPEND ␈↓and ␈↓¬ISTOT-APPEND ␈↓since we are in the case ␈↓↓¬␈↓αn|␈↓↓u␈↓
␈↓ ↓H␈↓62␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓␈↓ ∧\␈↓↓␈↓αa|␈↓↓u . [[␈↓αd|␈↓↓u] * [v * w]] = u * [v * w] ␈↓.
␈↓ ↓H␈↓Combining these two steps with the induction axiom we have the desired result.
␈↓ ↓H␈↓ In␈αthe␈αabove␈αproofs␈αwe␈αhave␈αbeen␈αparticularly␈αcareful␈αwith␈αdetails␈αsuch␈αas␈αshowing␈α␈↓↓islist␈αx␈↓
␈↓ ↓H␈↓where␈α
␈↓↓x␈↓␈α
is␈α
an␈α
expression␈α
to␈α
be␈α
substituted␈α∞for␈α
␈↓↓u␈↓␈α
in␈α
some␈α
sentence.␈α
The␈α
proofs␈α
are␈α
in␈α∞fact␈α
fairly
␈↓ ↓H␈↓close␈αto␈αthe␈αformal␈α
proofs␈αgiven␈αin␈α Appendix␈αA.␈α
In␈αlater␈αexamples␈αwe␈α
shall␈αbe␈αmore␈αbrief␈αas␈α
the
␈↓ ↓H␈↓reader will be expected to be able to fill in the details.
␈↓ ↓H␈↓8. ␈↓αPseudo-logic.␈↓
␈↓ ↓H␈↓ In␈αthis␈αsection␈αwe␈αgive␈αa␈α
method␈αfor␈αhandling␈αrecursively␈αdefined␈αpredicates.␈α Consider␈α
the
␈↓ ↓H␈↓recursive program
␈↓ ↓H␈↓␈↓ β:␈↓↓occur[x, y] ← [x = y] ∨ [¬␈↓αat|␈↓↓y ∧ [occur[x, ␈↓αa|␈↓↓y] ∨ occur[x, ␈↓αd|␈↓↓y]]]␈↓.
␈↓ ↓H␈↓If␈α⊃we␈α⊃were␈α⊃to␈α⊃treat␈α⊃this␈α⊃program␈α∩in␈α⊃a␈α⊃manner␈α⊃analogous␈α⊃to␈α⊃the␈α⊃functional␈α∩equation␈α⊃method
␈↓ ↓H␈↓described in the last section, we would represent the predicate ␈↓↓occur␈↓ by the sentence
␈↓ ↓H␈↓␈↓ β∂␈↓↓∀x y: [occur[x, y] ≡ [x = y] ∨ [¬␈↓αat|␈↓↓y ∧ [occur[x, ␈↓αa|␈↓↓y] ∨ occur[x, ␈↓αd|␈↓↓y]]]]␈↓.
␈↓ ↓H␈↓However,␈α
in␈α
order␈α
to␈α
justify␈α
this␈α
representation␈α
we␈α
would␈α
have␈α
to␈α
use␈α
some␈α
kind␈α
of␈α
logic␈α
of␈α
partial
␈↓ ↓H␈↓predicates,␈α⊂because␈α∂we␈α⊂could␈α∂not␈α⊂be␈α∂sure␈α⊂in␈α∂advance␈α⊂that␈α∂␈↓↓occur␈↓␈α⊂is␈α∂total,␈α⊂and␈α⊂honest␈α∂predicates
␈↓ ↓H␈↓cannot take the value ␈↓π|␈↓; they can only be true or false.
␈↓ ↓H␈↓ Instead,␈α∞we␈α
introduce␈α∞a␈α∞domain␈α
TV␈α∞of␈α
truth␈α∞values␈α∞consisting␈α
of␈α∞the␈α
elements␈α∞␈↓¬TT␈α∞␈↓and␈α
␈↓¬FF
␈↓ ↓H␈↓¬␈↓and␈α
having␈α
characteristic␈α
predicate␈α
␈↓↓istv.␈↓␈α
The␈α
extension,␈αTV␈↓∧+␈↓,␈α
of␈α
TV␈α
we␈α
shall␈α
call␈α
ETV.␈αIt␈α
consists
␈↓ ↓H␈↓of␈α
the␈α
elements␈α
␈↓¬TT,␈α
␈↓␈↓¬FF,␈α␈↓␈α
and␈α
␈↓π|␈↓␈α
and␈α
has␈α
characteristic␈αpredicate␈α
␈↓↓isetv.␈↓␈α
By␈α
identifying␈α
␈↓¬TT␈α
␈↓and␈α␈↓¬FF
␈↓ ↓H␈↓¬␈↓with␈α
appropriate␈α
LISP␈αatoms␈α
TV␈α
could␈αbe␈α
considered␈α
as␈αa␈α
subdomain␈α
of␈αSE.␈α
For␈α
our␈αpurposes␈α
it
␈↓ ↓H␈↓makes␈αno␈αdifference.␈α We␈αdefine␈αpseudo-logical␈αoperators␈α(␈↓↓nnot,␈↓␈α␈↓↓aand,␈↓␈α␈↓↓oor)␈↓␈αto␈αimitate␈αthe␈αlogical
␈↓ ↓H␈↓operators (¬, ∧, ∨) and pseudo-predicates (␈↓↓eeq,␈↓ ␈↓↓aatom)␈↓ to imitate the basic LISP predicates (=, ␈↓αat␈↓).
␈↓ ↓H␈↓ Now we can imitate the predicate ␈↓↓occur␈↓ by the recursive program
␈↓ ↓H␈↓␈↓ α@␈↓↓occura[x, y] ← [x eeq y] oor [nnot aatom y aand [occura[x, ␈↓αa|␈↓↓y] oor occura[x, ␈↓αd|␈↓↓y]]]␈↓
␈↓ ↓H␈↓which has the functional equation
␈↓ ↓H␈↓␈↓ α⊃␈↓↓∀x y: [occura[x, y] = [x eeq y] oor [nnot aatom y aand [occura[x, ␈↓αa|␈↓↓y] oor occura[x, ␈↓αd|␈↓↓y]]]]␈↓.
␈↓ ↓H␈↓Using this equation and the S-expression induction axiom we can show
␈↓ ↓H␈↓␈↓ ∧O␈↓↓∀x y: istv occura[x, y] ␈↓
␈↓ ↓H␈↓which is the statement that occura is total. If we define ␈↓↓occur␈↓ by
␈↓ ↓H␈↓␈↓ ∧G␈↓↓∀x y: [occur[x, y] ≡ occura[x, y] = ␈↓¬TT␈↓↓] ␈↓
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *63
␈↓ ↓H␈↓then we can prove the original sentence proposed for representing ␈↓↓occur.␈↓
␈↓ ↓H␈↓αETV axioms
␈↓ ↓H␈↓ We␈α
now␈αgive␈α
the␈αaxioms␈α
for␈αextended␈α
truths␈αvalues␈α
needed␈αto␈α
make␈αthe␈α
above␈αarguments
␈↓ ↓H␈↓and similar ones work. First the axioms describing the domains TV and ETV.
␈↓ ↓H␈↓␈↓¬TVSORTS: ␈↓ ␈↓↓etv,␈↓ ␈↓↓tv␈↓
␈↓ ↓H␈↓␈↓¬VARIABLES: ␈↓
␈↓ ↓H␈↓ ␈↓¬SORTED: ␈↓ ␈↓↓P, Q ε etv␈↓
␈↓ ↓H␈↓ ␈↓↓p, q ε tv␈↓
␈↓ ↓H␈↓␈↓¬B2: ␈↓ ␈↓↓∀p: isetv p␈↓
␈↓ ↓H␈↓␈↓¬B3: ␈↓ ␈↓↓isetv ␈↓π|␈↓↓ ∧ istv ␈↓¬TT ␈↓↓∧ istv ␈↓¬FF ␈↓↓␈↓
␈↓ ↓H␈↓␈↓¬B4: ␈↓ ␈↓↓∀P: [P = ␈↓¬TT ␈↓↓∨ P = ␈↓¬FF ␈↓↓∨ P = ␈↓π|␈↓↓]␈↓
␈↓ ↓H␈↓␈↓¬B5: ␈↓ ␈↓↓∀p: [p = ␈↓¬TT ␈↓↓∨ p = ␈↓¬FF␈↓↓]␈↓
␈↓ ↓H␈↓␈↓¬B6: ␈↓ ␈↓↓␈↓¬TT ␈↓↓≠ ␈↓¬FF ␈↓↓∧ ␈↓¬TT ␈↓↓≠ ␈↓π|␈↓↓ ∧ ␈↓¬FF ␈↓↓≠ ␈↓π|␈↓↓␈↓
␈↓ ↓H␈↓ Next␈α∪the␈α∪axioms␈α∪defining␈α∪the␈α∪functions␈α∪on␈α∪ETV␈α∪corresponding␈α∪to␈α∪the␈α∩pseudo-logical
␈↓ ↓H␈↓operators and pseudo-predicates.
␈↓ ↓H␈↓␈↓¬B7: ␈↓ ␈↓↓∀P: [nnot P = ␈↓αif␈↓↓ [P = ␈↓π|␈↓↓] ␈↓αthen␈↓↓ ␈↓π|␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ [P = ␈↓¬TT␈↓↓] ␈↓αthen␈↓↓ ␈↓¬FF ␈↓↓␈↓αelse␈↓↓ ␈↓¬TT␈↓↓]␈↓
␈↓ ↓H␈↓␈↓¬B8: ␈↓ ␈↓↓∀P Q: [P aand Q = ␈↓αif␈↓↓ [P = ␈↓π|␈↓↓] ␈↓αthen␈↓↓ ␈↓π|␈↓↓ ␈↓αelse␈↓↓ if [P = ␈↓¬TT␈↓↓] ␈↓αthen␈↓↓ Q ␈↓αelse␈↓↓ ␈↓¬FF␈↓↓]␈↓
␈↓ ↓H␈↓␈↓¬B9: ␈↓ ␈↓↓∀P Q: [P oor Q = ␈↓αif␈↓↓ [P = ␈↓π|␈↓↓] ␈↓αthen␈↓↓ ␈↓π|␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ [P = ␈↓¬TT␈↓↓] ␈↓αthen␈↓↓ ␈↓¬TT ␈↓↓␈↓αelse␈↓↓ Q]␈↓
␈↓ ↓H␈↓␈↓¬B10: ␈↓ ␈↓↓∀X Y: [X eeq Y] = ␈↓αif␈↓↓ [¬issexp X ∨ ¬issexp Y] ␈↓αthen␈↓↓ ␈↓π|␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ [X = Y] ␈↓αthen␈↓↓ ␈↓¬TT ␈↓↓␈↓αelse␈↓↓ ␈↓¬FF␈↓↓]␈↓
␈↓ ↓H␈↓␈↓¬B11: ␈↓ ␈↓↓∀X: [aatom X = ␈↓αif␈↓↓ [¬issexp X] ␈↓αthen␈↓↓ ␈↓π|␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ ␈↓¬TT ␈↓↓␈↓αelse␈↓↓ ␈↓¬FF␈↓↓]␈↓
␈↓ ↓H␈↓ Finally a conditional function which takes elements of ETV as its first argument.
␈↓ ↓H␈↓␈↓¬B12: ␈↓ ␈↓↓∀P X Y: [iif[P, X, Y] = ␈↓αif␈↓↓ [P = ␈↓π|␈↓↓] ␈↓αthen␈↓↓ ␈↓π|␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ [P = ␈↓¬TT␈↓↓] ␈↓αthen␈↓↓ X ␈↓αelse␈↓↓ Y]␈↓
␈↓ ↓H␈↓αETV lemmas
␈↓ ↓H␈↓ We␈α
now␈αgive␈α
some␈α
lemmas␈αwhich␈α
show␈αthat␈α
the␈α
pseudo-␈αoperators␈α
and␈α
predicates␈αbehave
␈↓ ↓H␈↓as advertised. First we need to know that the functions map into ETV.
␈↓ ↓H␈↓␈↓¬ETVNNOT: ␈↓ ␈↓↓∀P: isetv nnot P␈↓
␈↓ ↓H␈↓␈↓¬ETVAAND: ␈↓ ␈↓↓∀P Q: isetv [P aand Q]␈↓
␈↓ ↓H␈↓64␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓␈↓¬ETVOOR: ␈↓ ␈↓↓∀P Q: isetv [P oor Q]␈↓
␈↓ ↓H␈↓␈↓¬ETVEEQ: ␈↓ ␈↓↓∀X Y: isetv [X eeq Y]␈↓
␈↓ ↓H␈↓␈↓¬ETVAATOM: ␈↓ ␈↓↓∀X: isetv aatom X␈↓
␈↓ ↓H␈↓ These␈α⊃lemmas␈α⊃are␈α⊃all␈α⊃a␈α⊃direct␈α⊃consequence␈α⊃of␈α⊃the␈α⊃function␈α⊃definitions␈α⊃and␈α∩the␈α⊃axioms
␈↓ ↓H␈↓␈↓¬SORTED, ␈↓␈↓¬B2 ␈↓and ␈↓¬B3. ␈↓
␈↓ ↓H␈↓ Next␈αare␈αsome␈αlemmas␈αstating␈αthat␈αthe␈αfunctions␈αdefined␈αabove␈αare␈αtotal␈αwhen␈αrestricted␈αto
␈↓ ↓H␈↓the domains TV or SE as appropriate.
␈↓ ↓H␈↓␈↓¬TVNNOT: ␈↓ ␈↓↓∀p: istv nnot p␈↓
␈↓ ↓H␈↓␈↓¬TVAAND: ␈↓ ␈↓↓∀p q: istv [p aand q]␈↓
␈↓ ↓H␈↓␈↓¬TVOOR: ␈↓ ␈↓↓∀p q: istv [p oor q]␈↓
␈↓ ↓H␈↓␈↓¬TVEEQ: ␈↓ ␈↓↓∀x y: istv [x eeq y]␈↓
␈↓ ↓H␈↓␈↓¬TVAATOM: ␈↓ ␈↓↓∀x: istv atom x␈↓
␈↓ ↓H␈↓ These lemmas follow from the function definitions and the axioms ␈↓¬SORTED, ␈↓ ␈↓¬B3, ␈↓␈↓¬B5 ␈↓and ␈↓¬B6. ␈↓
␈↓ ↓H␈↓ Next␈α∩are␈α∩some␈α∪lemmas␈α∩which␈α∩show␈α∪that␈α∩the␈α∩functions␈α∪do␈α∩indeed␈α∩imitate␈α∪their␈α∩logical
␈↓ ↓H␈↓counterparts.
␈↓ ↓H␈↓␈↓¬EQNNOT: ␈↓ ␈↓↓∀p: [nnot p = ␈↓¬TT ␈↓↓≡ ¬[p = ␈↓¬TT␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓¬EQAAND: ␈↓ ␈↓↓∀p q: [p aand q = ␈↓¬TT ␈↓↓≡ p = ␈↓¬TT ␈↓↓∧ q = ␈↓¬TT␈↓↓]␈↓
␈↓ ↓H␈↓␈↓¬EQOOR: ␈↓ ␈↓↓∀p q: [p oor q = ␈↓¬TT ␈↓↓≡ p = ␈↓¬TT ␈↓↓∨ q = ␈↓¬TT␈↓↓]␈↓
␈↓ ↓H␈↓␈↓¬EQEEQ: ␈↓ ␈↓↓∀x y: [x eeq y = ␈↓¬TT ␈↓↓≡ x = y]␈↓
␈↓ ↓H␈↓␈↓¬EQAATOM: ␈↓ ␈↓↓∀x: [aatom x = ␈↓¬TT ␈↓↓≡ ␈↓αat|␈↓↓x]␈↓
␈↓ ↓H␈↓ These␈αlemmas␈αcan␈αall␈αbe␈αproved␈αby␈α
simple␈αcase␈αanalysis.␈α We␈αprove␈α␈↓¬EQAAND␈αand␈α
␈↓␈↓¬EQEEQ␈α␈↓as
␈↓ ↓H␈↓examples. By ␈↓¬SORTED, ␈↓␈↓¬B5, ␈↓␈↓¬B6, ␈↓and ␈↓¬B8 ␈↓ we have
␈↓ ↓H␈↓␈↓ ∧I␈↓↓p aand q = ␈↓αif␈↓↓ [p=␈↓¬TT] ␈↓↓␈↓αthen␈↓↓ q ␈↓αelse␈↓↓ ␈↓¬FF ␈↓↓ ␈↓
␈↓ ↓H␈↓and ␈↓¬EQAAND ␈↓follows immediately using ␈↓¬B6. ␈↓ By ␈↓¬SORTED ␈↓and ␈↓¬B10 ␈↓
␈↓ ↓H␈↓␈↓ ∧T␈↓↓x eeq y = ␈↓αif␈↓↓ [x = y] ␈↓αthen␈↓↓ ␈↓¬TT ␈↓↓␈↓αelse␈↓↓ ␈↓¬FF ␈↓↓ ␈↓
␈↓ ↓H␈↓and ␈↓¬EQEEQ ␈↓follows using ␈↓¬B6. ␈↓
␈↓ ↓H␈↓ Finally we give some lemmas that will be useful in later examples.
␈↓ ↓H␈↓␈↓¬TVNOTATM: ␈↓ ␈↓↓∀x y: istv [nnot aatom x aand nnot aatom y]␈↓
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *65
␈↓ ↓H␈↓␈↓¬EQNOTATM: ␈↓ ␈↓↓∀x y: [[nnot aatom x aand nnot aatom y] = ␈↓¬TT ␈↓↓≡ [¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y]]␈↓
␈↓ ↓H␈↓ ␈↓¬TVNOTATM␈α␈↓follows␈αfrom␈α␈↓¬TVNNOT,␈α␈↓␈↓¬TVAATOM,␈α␈↓and␈α␈↓¬TVAAND.␈α␈↓␈α␈↓¬EQNOTATM␈α␈↓follows␈αfrom␈α␈↓¬EQAAND
␈↓ ↓H␈↓¬␈↓and ␈↓↓∀x: nnot aatom x = ␈↓¬TT␈↓↓ ≡ ¬␈↓αat|␈↓↓x␈↓. The latter follows from ␈↓¬TVAATM, ␈↓ ␈↓¬EQNNOT ␈↓and ␈↓¬EQAATOM. ␈↓
␈↓ ↓H␈↓␈↓¬POORF: ␈↓ ␈↓↓∀P: [P oor ␈↓¬FF ␈↓↓= P]␈↓
␈↓ ↓H␈↓␈↓¬FAANDQ: ␈↓ ␈↓↓∀Q: [␈↓¬FF ␈↓↓aand Q = ␈↓¬FF␈↓↓]␈↓
␈↓ ↓H␈↓ ␈↓¬POORF␈α
␈↓follows␈αfrom␈α
␈↓¬B9␈α␈↓by␈α
case␈α
analysis␈αusing␈α
␈↓¬B4␈α␈↓and␈α
␈↓¬B6.␈α
␈↓␈α␈↓¬FAANDQ␈α
␈↓follows␈α by␈α
simplification
␈↓ ↓H␈↓of ␈↓¬B8 ␈↓using ␈↓¬B6. ␈↓
␈↓ ↓H␈↓ A formal proof of many of the above lemmas is included in Appendix A.
␈↓ ↓H␈↓α␈↓ εExercise.
␈↓ ↓H␈↓ Prove the statements made about ␈↓↓occur␈↓ and ␈↓↓occura.␈↓ Namely show
␈↓ ↓H␈↓␈↓ ∧O␈↓↓∀x y: istv occura[x, y] ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ β∂␈↓↓∀x y: [occur[x, y] ≡ [x = y] ∨ [¬␈↓αat|␈↓↓y ∧ [occur[x, ␈↓αa|␈↓↓y] ∨ occur[x, ␈↓αd|␈↓↓y]]]]␈↓.
␈↓ ↓H␈↓using the definitions
␈↓ ↓H␈↓␈↓ α∀␈↓↓∀x y: [occura[x, y] = [x eeq y] oor [nnot aatom y aand [occura[x, ␈↓αa|␈↓↓y] oor occura[x, ␈↓αd|␈↓↓y]]]]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ∧F␈↓↓∀x y: [occur[x, y] ≡ occura[x, y] = ␈↓¬TT␈↓↓]. ␈↓
␈↓ ↓H␈↓9. ␈↓αAn Extended Example.␈↓
␈↓ ↓H␈↓ As␈α
a␈α
non␈α∞trivial␈α
application␈α
of␈α∞the␈α
techniques␈α
described␈α
in␈α∞the␈α
previous␈α
sections,␈α∞we␈α
will
␈↓ ↓H␈↓give␈αa␈αproof␈αof␈αcorrectness␈αof␈αthe␈αpredicate␈α␈↓↓samefringe␈↓␈αwhich␈αhas␈αbeen␈αproposed␈αas␈αa␈αsolution␈αof
␈↓ ↓H␈↓the␈α∪SAMEFRINGE␈α∀problem.␈α∪ This␈α∀is␈α∪the␈α∀problem␈α∪of␈α∀determining␈α∪whether␈α∀or␈α∪not␈α∀two␈α∪S-
␈↓ ↓H␈↓expressions␈α
have␈α
the␈αsame␈α
fringe␈α
using␈αa␈α
minimum␈α
of␈αspace.␈α
(We␈α
will␈αbe␈α
concerned␈α
only␈αwith␈α
the
␈↓ ↓H␈↓correctness of ␈↓↓samefringe␈↓ since the efficiency is not an ␈↓↓extensional␈↓ property).
␈↓ ↓H␈↓ The␈α
fringe␈α
of␈α
an␈αS-expression,␈α
␈↓↓x,␈↓␈α
is␈α
a␈α
list␈αof␈α
atoms␈α
in␈α
the␈α
order␈αthat␈α
they␈α
occur␈α
in␈α␈↓↓x.␈↓␈α
Thus
␈↓ ↓H␈↓the␈α∂fringe␈α⊂of␈α∂␈↓¬A␈α⊂␈↓is␈α∂␈↓¬(A)␈α⊂␈↓and␈α∂the␈α∂fringe␈α⊂of␈α∂␈↓¬((A . B) . (C . D))␈↓␈α⊂is␈α∂␈↓¬(A B C D)␈↓.␈α⊂ The␈α∂LISP␈α⊂program␈α∂for
␈↓ ↓H␈↓computing the fringe of an S-expression is
␈↓ ↓H␈↓9.1)␈↓ βX␈↓↓fringe x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <x> ␈↓αelse␈↓↓ [fringe ␈↓αa|␈↓↓x] * [fringe ␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓The predicate ␈↓↓samefringe␈↓ is computed by the LISP program
␈↓ ↓H␈↓66␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓9.2) ␈↓ β∧␈↓↓samefringe[x, y] ← x = y ∨ [¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y ∧ same[gopher x, gopher y]]␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓9.3) ␈↓ ∧,␈↓↓same[x, y] ← ␈↓αa|␈↓↓x = ␈↓αa|␈↓↓y ∧ samefringe[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓9.4) ␈↓ βT␈↓↓gopher x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ gopher [␈↓αaa|␈↓↓x . [␈↓αda|␈↓↓x . ␈↓αd|␈↓↓x]]␈↓
␈↓ ↓H␈↓Using the method of the previous section we define an imitation of ␈↓↓samefringe␈↓ by
␈↓ ↓H␈↓␈↓ β∃␈↓↓samefringea[x, y] ← [x eeq y] oor [[nnot aatom x aand nnot aatom y]␈↓
␈↓ ↓H␈↓9.5) ␈↓ ∧<␈↓↓ aand samea[gopher x, gopher y]]␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓9.6) ␈↓ βr␈↓↓samea[x, y] ← [␈↓αa|␈↓↓x eeq ␈↓αa|␈↓↓y] aand samefringea[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y]␈↓
␈↓ ↓H␈↓Thus we have the functional equations
␈↓ ↓H␈↓␈↓¬FRINGE: ␈↓ ␈↓↓∀x: [fringe x = ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <x> ␈↓αelse␈↓↓ [fringe ␈↓αa|␈↓↓x] * [fringe ␈↓αd|␈↓↓x]]␈↓
␈↓ ↓H␈↓␈↓¬SAMEFRINGEA: ␈↓ ␈↓↓∀x y: [samefringea[x, y] = [x eeq y] oor [[nnot aatom x aand nnot aatom y]␈↓
␈↓ ↓H␈↓ ␈↓↓aand samea[gopher x, gopher y]]]␈↓
␈↓ ↓H␈↓␈↓¬SAMEA: ␈↓ ␈↓↓∀x y: [samea[x, y] = [␈↓αa|␈↓↓x eeq ␈↓αa|␈↓↓y] aand samefringea[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y]]␈↓
␈↓ ↓H␈↓␈↓¬SAMEFRINGE: ␈↓ ␈↓↓∀x y: [samefringe[x, y] ≡ samefringea[x, y] = ␈↓¬TT␈↓↓]␈↓
␈↓ ↓H␈↓␈↓¬SAME: ␈↓ ␈↓↓∀x y: [same[x, y] ≡ samea[x, y] = ␈↓¬TT␈↓↓]␈↓
␈↓ ↓H␈↓␈↓¬GOPHER: ␈↓ ␈↓↓∀x: [gopher x = ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ gopher [␈↓αaa|␈↓↓x . [␈↓αda|␈↓↓x . ␈↓αd|␈↓↓x]]]␈↓
␈↓ ↓H␈↓ In␈αorder␈α
to␈αprove␈α
that␈α␈↓↓samefringea␈↓␈α
is␈αtotal␈α
we␈αwill␈α
need␈αthe␈α
fact␈αthat␈α
␈↓↓samea␈↓␈αmaps␈αinto␈α
ETV.
␈↓ ↓H␈↓This␈α is␈α because␈α in␈α the␈α∨case␈α ␈↓↓x␈↓␈α is␈α an␈α atom␈α or␈α∨␈↓↓y␈↓␈α is␈α an␈α atom␈α we␈α must␈α∨have
␈↓ ↓H␈↓␈↓↓isetv samea[gopher x, gopher y]␈↓␈α⊂in␈α∂order␈α⊂to␈α∂use␈α⊂the␈α⊂definition␈α∂of␈α⊂␈↓↓aand␈↓␈α∂even␈α⊂though␈α⊂the␈α∂actual
␈↓ ↓H␈↓value␈αis␈αirrelevant.␈αThere␈αare␈αseveral␈αways␈αto␈αdo␈α
this.␈α One␈αwould␈αbe␈αto␈αdefine␈α␈↓↓gopher␈↓␈αof␈αan␈α
atom
␈↓ ↓H␈↓to␈αbe␈α␈↓π|␈↓,␈αand␈αhave␈αan␈α
axiom␈αstating␈αthat␈α␈↓↓samea␈↓␈αproduces␈α␈↓π|␈↓␈α
if␈αeither␈αargument␈αis␈α␈↓π|␈↓.␈α Another␈αis␈α
to
␈↓ ↓H␈↓take␈α
as␈αpart␈α
of␈αthe␈α
definition␈α
of␈α␈↓↓samea␈↓␈α
that␈αit␈α
maps␈α
into␈αETV,␈α
without␈αstating␈α
for␈αwhat␈α
arguments
␈↓ ↓H␈↓it␈α
actually␈α
produces␈α
␈↓π|␈↓.␈α
We␈α
have␈α
chosen␈α
the␈αlatter␈α
option␈α
as␈α
it␈α
seems␈α
cleaner.␈α
Thus␈α
we␈α
add␈αthe
␈↓ ↓H␈↓axiom
␈↓ ↓H␈↓␈↓¬ETVSAMEA: ␈↓ ␈↓↓∀X Y isetv samea[X, Y]␈↓
␈↓ ↓H␈↓ The goal is to prove the following three statements:
␈↓ ↓H␈↓␈↓¬THM1: ␈↓ ␈↓↓∀x y: istv samefringea[x, y]␈↓
␈↓ ↓H␈↓␈↓¬THM2: ␈↓ ␈↓↓∀x y: [samefringe[x, y] ≡ [x = y] ∨ [[¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y] ∧ same[gopher x, gopher y]]␈↓
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *67
␈↓ ↓H␈↓␈↓¬THM3: ␈↓ ␈↓↓∀x y: [samefringe[x, y] ≡ fringe x = fringe y]␈↓
␈↓ ↓H␈↓ ␈↓¬THM1␈α␈↓says␈αthat␈α␈↓↓samefringea␈↓␈αis␈αtotal,␈α␈↓¬THM2␈α␈↓says␈αthat␈α␈↓↓samefringe␈↓␈αas␈αdefined␈αvia␈αits␈αimitatiom
␈↓ ↓H␈↓satisfies the intended equivalence, and ␈↓¬THM3 ␈↓says that ␈↓↓samefringe␈↓ is correct.
␈↓ ↓H␈↓ Due␈α⊂to␈α⊂the␈α⊂complicated␈α⊂form␈α∂of␈α⊂the␈α⊂recursion␈α⊂defining␈α⊂␈↓↓samefringe,␈↓␈α⊂simple␈α∂S-expression
␈↓ ↓H␈↓induction␈α∞is␈α∞not␈α∞adequate␈α∞to␈α∞prove␈α∞the␈α∞above␈α
theorems.␈α∞ Therefore␈α∞if␈α∞we␈α∞wish␈α∞to␈α∞carry␈α∞out␈α
the
␈↓ ↓H␈↓proof␈αby␈α
structural␈αinduction␈α
we␈αmust␈α
find␈αan␈αaxiom␈α
schema␈αappropriate␈α
for␈αthe␈α
problem.␈α One
␈↓ ↓H␈↓way␈αis␈αto␈αuse␈αinduction␈αon␈αthe␈αsize␈αof␈αthe␈αS-expressions␈αinvolved.␈α The␈αsize␈αof␈αan␈αS-expression␈α␈↓↓x␈↓
␈↓ ↓H␈↓is the number of atoms occuring in ␈↓↓x␈↓ and is computed by the LISP program
␈↓ ↓H␈↓9.7) ␈↓ ∧,␈↓↓size x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x␈↓
␈↓ ↓H␈↓␈↓↓size␈↓␈α
provides␈α
a␈α
mapping␈α
of␈α
S-expressions␈α
into␈α
the␈α
domain␈α
of␈α
natural␈α
numbers␈α
and␈α
allows␈α
us␈α
to
␈↓ ↓H␈↓transform␈α
the␈α
problem␈αinto␈α
one␈α
of␈α
natural␈αnumber␈α
induction.␈α
In␈α
particular␈αwe␈α
will␈α
take␈α
for␈αthe
␈↓ ↓H␈↓axiom schema, the course of values induction schema
␈↓ ↓H␈↓␈↓¬NUMBINDUCTION: ␈↓ ␈↓↓∀n: [∀m: [m < n ⊃ ␈↓πF␈↓↓ m] ⊃ ␈↓πF␈↓↓ n] ⊃ ∀n: ␈↓πF␈↓↓ n␈↓
␈↓ ↓H␈↓with
␈↓ ↓H␈↓␈↓ ∧∨␈↓↓␈↓πF␈↓↓ n ≡ ∀x y: [size x + size y = n ⊃ THM[x, y]]␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓¬THM: ␈↓ ␈↓↓∀x y: [THM[x, y] ≡ THM1[x, y] ∧ THM2[x, y] ∧ THM3[x, y]]␈↓.
␈↓ ↓H␈↓ In␈α
order␈α
to␈α
use␈α
the␈α
␈↓¬NUMBINDUCTION␈α
␈↓␈α
axiom␈α
we␈α
will␈α
need␈α
the␈α
functional␈α
equation␈α
for␈α␈↓↓size␈↓␈α
and
␈↓ ↓H␈↓some␈α∞properties␈α∞of␈α∞the␈α∞natural␈α∞numbers.␈α∞ The␈α∞characteristic␈α∞predicate␈α∞of␈α∞the␈α∞domain␈α∞of␈α∞natural
␈↓ ↓H␈↓numbers is ␈↓↓natnum␈↓ and we add to our list of axioms the following:
␈↓ ↓H␈↓␈↓¬NUMVAR: ␈↓ ␈↓↓∀k l m n: [natnum k ∧ natnum l ∧ natnum m ∧ natnum n]␈↓
␈↓ ↓H␈↓␈↓¬ONE: ␈↓ ␈↓↓natnum 1␈↓
␈↓ ↓H␈↓␈↓¬ISTOT-PLUS: ␈↓ ␈↓↓∀m n: natnum [m + n]␈↓
␈↓ ↓H␈↓␈↓¬ASSOC: ␈↓ ␈↓↓∀l m n: l + [m + n] = [l + m] + n␈↓
␈↓ ↓H␈↓␈↓¬ORDER-PLUS: ␈↓ ␈↓↓∀k l m n: [[k < l ∧ m < n] ⊃ k + m < l + n]␈↓
␈↓ ↓H␈↓␈↓¬ORDER: ␈↓ ␈↓↓∀n: n < 1 + n␈↓
␈↓ ↓H␈↓␈↓¬SIZE: ␈↓ ␈↓↓∀x: [size x = ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓Here␈αwe␈αhave␈αnot␈αattempted␈αto␈αaxiomatize␈αthe␈αnatural␈αnumbers,␈αbut␈αsimply␈αhave␈αtaken␈αas␈α
axioms
␈↓ ↓H␈↓those properties needed for the present problem.
␈↓ ↓H␈↓ Now␈α∞to␈α
proceed␈α∞with␈α
the␈α∞proof.␈α
In␈α∞addition␈α
to␈α∞the␈α
lemmas␈α∞ about␈α
␈↓↓append␈↓␈α∞and␈α∞the␈α
ETV
␈↓ ↓H␈↓lemmas␈α⊂proved␈α∂in␈α⊂earlier␈α⊂sections,␈α∂we␈α⊂will␈α⊂need␈α∂some␈α⊂lemmas␈α⊂about␈α∂␈↓↓gopher,␈↓␈α⊂␈↓↓fringe,␈↓␈α⊂␈↓↓size␈↓␈α∂and
␈↓ ↓H␈↓combinations thereof. They are
␈↓ ↓H␈↓68␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓␈↓¬GOOD-GOPHER: ␈↓ ␈↓↓∀x: [¬␈↓αat|␈↓↓x ⊃ [issexp gopher x ∧ issexp ␈↓αa|␈↓↓gopher x ∧ issexp ␈↓αd|␈↓↓gopher x]]␈↓
␈↓ ↓H␈↓␈↓¬GOOD-FRINGE: ␈↓ ␈↓↓∀x: [issexp fringe x ∧ ¬␈↓αat|␈↓↓fringe x]␈↓
␈↓ ↓H␈↓␈↓¬FRINGE-ATM: ␈↓ ␈↓↓∀x y: [[␈↓αat|␈↓↓x ∨ ␈↓αat|␈↓↓y] ⊃ [fringe x = fringe y ≡ x = y]]␈↓
␈↓ ↓H␈↓␈↓¬FRINGE-GOPHER: ␈↓␈↓↓∀x: [¬␈↓αat|␈↓↓x ⊃ [␈↓αa|␈↓↓fringe x = ␈↓αa|␈↓↓gopher x] ∧ [␈↓αd|␈↓↓fringe x = fringe ␈↓αd|␈↓↓gopher x]]␈↓
␈↓ ↓H␈↓␈↓¬ISTOT-SIZE: ␈↓ ␈↓↓∀x: natnum size x␈↓
␈↓ ↓H␈↓␈↓¬SIZE-GOPHER: ␈↓ ␈↓↓∀x y: [[¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y] ⊃ [size ␈↓αd|␈↓↓gopher x +size ␈↓αd|␈↓↓gopher y] < [size x + size y]]␈↓
␈↓ ↓H␈↓ We␈α⊂will␈α⊃give␈α⊂only␈α⊃a␈α⊂brief␈α⊂indication␈α⊃of␈α⊂the␈α⊃proof␈α⊂of␈α⊂these␈α⊃lemmas.␈α⊂ Formal␈α⊃proofs␈α⊂are
␈↓ ↓H␈↓contained in Appendix A.
␈↓ ↓H␈↓ The␈αkey␈αidea␈αin␈αproving␈αthings␈αabout␈α␈↓↓gopher␈↓␈αis␈αto␈αfirst␈αprove␈αproperties␈αof␈α␈↓↓gopher[x . y]␈↓␈αas
␈↓ ↓H␈↓gopher␈αis␈α
only␈αdefined␈αfor␈α
non␈αatoms␈α
and␈αevery␈αnon␈α
atom␈αcan␈α
be␈αexpressed␈αas␈α
␈↓↓x . y␈↓␈αfor␈αsuitable␈α
␈↓↓x␈↓
␈↓ ↓H␈↓and␈α␈↓↓y.␈↓␈αFor␈αthis␈α
purpose␈αwe␈αstart␈αwith␈α
some␈αuseful␈αfacts␈αderived␈α
from␈α␈↓¬GOPHER,␈α␈↓␈α␈↓¬FRINGE,␈α
␈↓␈↓¬SIZE␈α␈↓and
␈↓ ↓H␈↓the LISP axioms.
␈↓ ↓H␈↓␈↓¬GOPHER-CONS-ATM: ␈↓ ␈↓↓∀x y: [␈↓αat|␈↓↓x ⊃ gopher[x . y] = [x . y]]␈↓
␈↓ ↓H␈↓␈↓¬GOPHER-CONS-NOTATM: ␈↓ ␈↓↓∀x y: [¬␈↓αat|␈↓↓x ⊃ gopher[x . y] = gopher[␈↓αa|␈↓↓x. [␈↓αd|␈↓↓x . y]]]␈↓
␈↓ ↓H␈↓␈↓¬FRINGE-CONS: ␈↓ ␈↓↓∀x y: [fringe[x . y] = fringe x * fringe y]␈↓
␈↓ ↓H␈↓␈↓¬SIZE-CONS: ␈↓ ␈↓↓∀x y: [size[x . y] = size x + size y]␈↓
␈↓ ↓H␈↓ To prove ␈↓¬GOOD-GOPHER ␈↓first prove, by a straight forward use of ␈↓¬SEXPINDUCTION, ␈↓that
␈↓ ↓H␈↓ ␈↓↓∀x y: [issexp gopher[x . y] ∧ ¬␈↓αat|␈↓↓gopher[x . y]]␈↓
␈↓ ↓H␈↓The lemma then follows from this and the axioms ␈↓¬CAR, ␈↓␈↓¬CDR1 ␈↓and ␈↓¬CONS1. ␈↓
␈↓ ↓H␈↓ ␈↓¬GOOD-FRINGE␈α∂␈↓folllows␈α∂from␈α∂ ␈↓↓∀x:␈α∂[islist␈α∞fringe␈α∂x␈α∂∧␈α∂¬␈↓αn|␈↓↓fringe␈α∂x]␈↓␈α∞,␈α∂␈↓¬SEXP1,␈α∂␈↓␈α∂and␈α∂␈↓¬NULL.␈α∂␈↓␈α∞The
␈↓ ↓H␈↓former is proved using ␈↓¬SEXPINDUCTION. ␈↓
␈↓ ↓H␈↓ For␈α⊃␈↓¬FRINGE-ATM␈α⊃␈↓we␈α⊂assume␈α⊃␈↓↓␈↓αat|␈↓↓x␈↓␈α⊃and␈α⊂show␈α⊃␈↓↓fringe x = fringe y ≡ x = y␈↓.␈α⊃ The␈α⊃lemma␈α⊂then
␈↓ ↓H␈↓follows␈αfrom␈αthe␈αsymmetry␈αof␈αthe␈αoccurrences␈αof␈α␈↓↓x␈↓␈αand␈α␈↓↓y.␈↓␈α From␈αthe␈αdefinition␈αof␈α␈↓↓fringe␈↓␈αwe␈αhave
␈↓ ↓H␈↓␈↓↓fringe x = <x> = [x . ␈↓¬NIL␈↓↓]␈↓.␈α
If␈α
␈↓↓␈↓αat␈↓↓ y␈↓␈α
the␈α
result␈α
follows␈α∞using␈α
␈↓¬EQ-SEXP.␈α
␈↓␈α
If␈α
␈↓↓¬␈↓αat␈↓↓ y␈↓␈α
then␈α
␈↓↓x ≠ y␈↓␈α∞and␈α
by
␈↓ ↓H␈↓␈↓¬EQ-SEXP␈α⊃␈↓we␈α⊃need␈α⊃only␈α⊂prove␈α⊃␈↓↓¬␈↓αn␈↓↓ ␈↓αd␈↓↓ fringe y␈↓.␈α⊃But␈α⊃this␈α⊂follows␈α⊃from␈α⊃␈↓¬CDR-APPEND␈α⊃␈↓and␈α⊂␈↓¬NOTNUL-
␈↓ ↓H␈↓¬APPEND. ␈↓
␈↓ ↓H␈↓ To prove ␈↓¬FRINGE-GOPHER ␈↓we use the "gopher trick" again and first prove
␈↓ ↓H␈↓␈↓ αK␈↓↓∀x y: [␈↓αa|␈↓↓fringe[x . y] = ␈↓αa|␈↓↓gopher[x . y] ∧ ␈↓αd|␈↓↓fringe[x . y] = fringe ␈↓αd|␈↓↓gopher[x . y]]␈↓
␈↓ ↓H␈↓using ␈↓¬SEPXINDUCTION. ␈↓ By ␈↓¬CAR-APPEND, ␈↓␈↓¬CDR-APPEND ␈↓and ␈↓¬FRINGE-CONS ␈↓we have
␈↓ ↓H␈↓␈↓ ∧Q␈↓↓␈↓αa|␈↓↓fringe[x . y] = ␈↓αa|␈↓↓fringe x, ␈↓
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *69
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ∧D␈↓↓␈↓αd|␈↓↓fringe[x . y] = [␈↓αd|␈↓↓fringe x] * fringe y. ␈↓
␈↓ ↓H␈↓In␈αthe␈αcase␈α␈↓↓␈↓αat|␈↓↓x␈↓␈α the␈αresult␈αfollows␈αfrom␈α␈↓¬NIL-APPEND,␈α␈↓␈↓¬GOPHER-CONS-ATM,␈α␈↓␈α and␈α␈↓↓fringe x = [x . ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓. In the case ␈↓↓¬␈↓αat␈↓↓ x␈↓ we show
␈↓ ↓H␈↓␈↓ ∧M␈↓↓fringe[x . y] = fringe[␈↓αa|␈↓↓x . [␈↓αd|␈↓↓x . y]] ␈↓
␈↓ ↓H␈↓using␈α∪properties␈α∪of␈α∪␈↓↓append␈↓␈α∪and␈α∪␈↓¬FRINGE-CONS.␈α∩␈↓␈α∪Then␈α∪the␈α∪result␈α∪follows␈α∪from␈α∪the␈α∩induction
␈↓ ↓H␈↓hypothesis␈α∃and␈α∀␈↓¬GOPHER-CONS-NOTATM.␈α∃␈↓␈α∃␈↓¬FRINGE-GOPHER␈α∀␈↓now␈α∃follows␈α∀from␈α∃the␈α∃above␈α∀using
␈↓ ↓H␈↓␈↓¬CONSTRUCT. ␈↓
␈↓ ↓H␈↓ ␈↓¬ISTOT-SIZE␈α␈↓is␈α
proved␈αby␈αa␈α
straight␈αforward␈α
application␈αof␈α␈↓¬SEXPINDUCTION.␈α
␈↓␈αTo␈α
prove␈α␈↓¬SIZE-
␈↓ ↓H␈↓¬GOPHER ␈↓we first show
␈↓ ↓H␈↓␈↓ ∧P␈↓↓∀x y: [size ␈↓αd|␈↓↓gopher[x . y] < size[x . y]]␈↓
␈↓ ↓H␈↓using ␈↓¬SEXPINDUCTION. ␈↓ In the case ␈↓↓␈↓αa|␈↓↓x␈↓ we have
␈↓ ↓H␈↓␈↓ β←␈↓↓size ␈↓αd|␈↓↓gopher[x . y] = size y␈↓ and ␈↓↓size[x . y] = 1 + size y␈↓
␈↓ ↓H␈↓by␈α␈↓¬SIZE,␈α␈↓␈↓¬SIZE-CONS␈α␈↓and␈α␈↓¬GOPHER-CONS-ATM␈α␈↓and␈αthe␈αresult␈αfollows␈αby␈α␈↓¬ORDER.␈αIn␈α␈↓the␈αcase␈α
␈↓↓¬␈↓αat␈↓↓ x␈↓␈αwe
␈↓ ↓H␈↓show
␈↓ ↓H␈↓␈↓ ¬↓␈↓↓size[x . y] = size[␈↓αa|␈↓↓x . [␈↓αd|␈↓↓x . y]]␈↓
␈↓ ↓H␈↓using␈α∪␈↓¬SIZE-CONS,␈α∩␈↓␈↓¬ASSOC␈α∪␈↓and␈α∩the␈α∪LISP␈α∩axioms.␈α∪The␈α∩result␈α∪then␈α∩follows␈α∪from␈α∪the␈α∩induction
␈↓ ↓H␈↓hypothesis␈α∞and␈α
␈↓¬GOPHER-CONS-NOTATM.␈α∞␈↓␈α
␈↓¬SIZE-GOPHER␈α∞␈↓now␈α
follows␈α∞from␈α
␈↓¬CONSTRUCT␈α∞␈↓and␈α
␈↓¬ORDER-
␈↓ ↓H␈↓¬PLUS. ␈↓
␈↓ ↓H␈↓ Now␈α∞we␈α
are␈α∞ready␈α
to␈α∞prove␈α∞the␈α
SAMEFRINGE␈α∞theorem.␈α
The␈α∞proof␈α
is␈α∞divided␈α∞into␈α
two
␈↓ ↓H␈↓cases. In the first case we assume ␈↓↓␈↓αat|␈↓↓x ∨ ␈↓αat|␈↓↓y␈↓. Then by the ETV lemmas
␈↓ ↓H␈↓␈↓ ∧Q␈↓↓nnot aatom x aand nnot aatom y = ␈↓¬FF ␈↓↓␈↓,
␈↓ ↓H␈↓by ␈↓¬FAANDP ␈↓and ␈↓¬ETVSAMEA ␈↓
␈↓ ↓H␈↓␈↓ ∧;␈↓↓␈↓¬FF ␈↓↓aand samea[gopher x, gopher y] = ␈↓¬FF ␈↓↓␈↓,
␈↓ ↓H␈↓and by ␈↓¬POORF ␈↓
␈↓ ↓H␈↓␈↓ ¬⊗␈↓↓[x eeq y] aand ␈↓¬FF ␈↓↓= x eeq y ␈↓.
␈↓ ↓H␈↓Thus by ␈↓¬SAMEFRINGEA ␈↓we have
␈↓ ↓H␈↓␈↓ ¬→␈↓↓samefringea[x, y] = x eeq y␈↓
␈↓ ↓H␈↓and ␈↓¬THM ␈↓now follows using the ETV lemmas, ␈↓¬FRINGE-ATM ␈↓and ␈↓¬SAMEFRINGE. ␈↓
␈↓ ↓H␈↓70␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓ In␈αthe␈αsecond␈αcase␈αwe␈αassume␈α␈↓↓¬␈↓αat|␈↓↓x␈α∧␈α¬␈↓αat|␈↓↓y␈↓␈αand␈αmake␈αuse␈αof␈αthe␈α␈↓¬NUMBINDCTION␈α␈↓axiom␈αand
␈↓ ↓H␈↓predicate as mentioned above. In particular we have the induction hypothesis
␈↓ ↓H␈↓␈↓ β?␈↓↓∀m: [m < n ⊃ ∀x␈↓β1␈↓↓ y␈↓β1␈↓↓: [size x␈↓β1␈↓↓ + size y␈↓β1␈↓↓ = m ⊃ THM[x␈↓β1␈↓↓, y␈↓β1␈↓↓]] ␈↓.
␈↓ ↓H␈↓Taking␈α∀ ␈↓↓n␈α∀=␈α∀size␈α∪x␈α∀+␈α∀size␈α∀y␈↓,␈α∀␈↓↓m␈α∪=␈α∀size␈α∀␈↓αd|␈↓↓gopher␈α∀x␈α∪+␈α∀size␈α∀␈↓αd|␈↓↓gopher␈α∀y␈↓,␈α∀␈↓↓x␈↓β1␈↓↓ = ␈↓αd␈↓↓ gopher x␈↓␈α∪and
␈↓ ↓H␈↓␈↓↓y␈↓β1␈↓↓ = ␈↓αd␈↓↓ gopher y␈↓ we have by ␈↓¬SIZE-GOPHER ␈↓
␈↓ ↓H␈↓␈↓¬THMCDRGO: ␈↓ ␈↓↓THM[␈↓αd|␈↓↓gopher x, ␈↓αd|␈↓↓gopher y]␈↓
␈↓ ↓H␈↓Assigning␈αthese␈αvalues␈αto␈α␈↓↓n␈↓␈αand␈α␈↓↓m␈↓␈αis␈αallowed␈αsince␈αby␈α␈↓¬ISTOT-PLUS,␈α␈↓␈α␈↓¬ISTOT-SIZE␈α␈↓and␈α␈↓¬GOOD-GOPHER
␈↓ ↓H␈↓¬␈↓the expressions satisfy ␈↓↓natnum.␈↓
␈↓ ↓H␈↓ ␈↓¬THM1␈α↔␈↓now␈α↔follows␈α↔from␈α↔␈↓¬THMCDRGO,␈α⊗␈↓the␈α↔ETV␈α↔lemmas,␈α↔␈↓¬GOOD-GOPHER,␈α↔␈↓␈↓¬SAMEA␈α↔␈↓␈α⊗and
␈↓ ↓H␈↓␈↓¬SAMEFRINGEA.␈α
␈↓␈α
␈↓¬THM2␈α
␈↓follows␈α
for␈αthe␈α
above␈α
reasons␈α
and␈α
␈↓¬SAME.␈α␈↓␈α
By␈α
␈↓¬SAMEA,␈α
␈↓␈α
␈↓¬SAMEFRINGE,␈α␈↓␈↓¬GOOD-
␈↓ ↓H␈↓¬GOPHER, ␈↓␈↓¬THMCDRGO ␈↓and the ETV lemmas it follows that
␈↓ ↓H␈↓␈↓ αj␈↓↓samea[gopher x, gopher y] = ␈↓¬TT ␈↓↓≡ ␈↓
␈↓ ↓H␈↓␈↓ β∨␈↓↓[␈↓αa|␈↓↓gopher x = ␈↓αa|␈↓↓gopher y] ∧ [fringe ␈↓αd|␈↓↓gopher x = fringe ␈↓αd|␈↓↓gopher y]␈↓.
␈↓ ↓H␈↓Using ␈↓¬FRINGE-GOPHER, ␈↓␈↓¬THM2, ␈↓and ␈↓¬SAME ␈↓we have
␈↓ ↓H␈↓␈↓ αO␈↓↓samefringe[x, y] ≡ x = y ∨ [[␈↓αa|␈↓↓fringe x = ␈↓αa|␈↓↓fringe y] ∧ [␈↓αd|␈↓↓fringe x = ␈↓αd|␈↓↓fringe y]]␈↓.
␈↓ ↓H␈↓Finally ␈↓¬THM3 ␈↓follows from ␈↓¬EQ-SEXP ␈↓and ␈↓¬GOOD-FRINGE. ␈↓
␈↓ ↓H␈↓ Combining the two cases and applying the induction axiom we conclude
␈↓ ↓H␈↓␈↓ ∧&␈↓↓∀n: ∀x y: [[size x + size y] = n ⊃ THM[x, y]]␈↓
␈↓ ↓H␈↓and by ␈↓¬ISTOT-SIZE ␈↓and ␈↓¬ISTOT-PLUS ␈↓it follows that
␈↓ ↓H␈↓␈↓ ¬⊃␈↓↓∀x y: THM[x, y] . ␈↓
␈↓ ↓H␈↓ In the above proof we could have equally well used the predicate
␈↓ ↓H␈↓␈↓ ∧T␈↓↓␈↓πF␈↓↓ n ≡ ∀x y: [size x = n ⊃ THM[x, y]]␈↓
␈↓ ↓H␈↓since the recursive call to ␈↓↓samefringe␈↓ always reduces the size of the first argument.
␈↓ ↓H␈↓ Another version of ␈↓↓samefringe␈↓ without any auxilliary functions is
␈↓ ↓H␈↓␈↓ β8␈↓↓samefringe[x, y] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ x = y ∨ ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ [¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y ∧ ␈↓
␈↓ ↓H␈↓9.8)␈↓ β8␈↓↓ [␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓x ␈↓αthen␈↓↓ [␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓y ␈↓αthen␈↓↓ ␈↓αa|␈↓↓x = ␈↓αa|␈↓↓y ∧ samefringe[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y] ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓αelse␈↓↓ samefringe[x, ␈↓αaa|␈↓↓y. [␈↓αda|␈↓↓y . ␈↓αd|␈↓↓y]]]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓αelse␈↓↓ samefringe[␈↓αaa|␈↓↓x . [␈↓αda|␈↓↓x . ␈↓αd|␈↓↓x], y]. ␈↓
␈↓ ↓H␈↓Note that a recursive call to ␈↓↓samefringe␈↓ does one of the following:
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *71
␈↓ ↓H␈↓ 1) decreases ␈↓↓size x + size y␈↓
␈↓ ↓H␈↓ 2) leaves ␈↓↓size x + size y␈↓ and ␈↓↓size ␈↓αa|␈↓↓x␈↓ invariant and decreases ␈↓↓size ␈↓αa|␈↓↓y␈↓
␈↓ ↓H␈↓or
␈↓ ↓H␈↓ 3) leaves ␈↓↓sixe x + size y␈↓ and ␈↓↓size ␈↓αa|␈↓↓y␈↓ invariant and decreases ␈↓↓size ␈↓αa|␈↓↓x ␈↓.
␈↓ ↓H␈↓This␈α∪can␈α∩lead␈α∪to␈α∪a␈α∩choice␈α∪of␈α∩an␈α∪induction␈α∪axiom␈α∩schema␈α∪in␈α∩at␈α∪least␈α∪two␈α∩ways.␈α∪ If␈α∪in␈α∩the
␈↓ ↓H␈↓␈↓¬NUMBINDUCTION␈α␈↓schema␈αwe␈αlet␈α␈↓↓n␈↓␈αand␈α␈↓↓m␈↓␈αrange␈αover␈αall␈αordinals␈αless␈αthan␈αa␈αgiven␈αone␈αit␈αbecomes␈αa
␈↓ ↓H␈↓schema␈α∩of␈α∩transfinite␈α∩induction.␈α⊃Ordinary␈α∩induction␈α∩is␈α∩obtained␈α⊃as␈α∩a␈α∩special␈α∩case␈α∩where␈α⊃the
␈↓ ↓H␈↓bounding␈α∞ordinal␈α∞is␈α∞␈↓πw␈↓␈α∞the␈α∞least␈α∞transfinite␈α∞ordinal.␈α∞ If␈α∞we␈α∞take␈α∞the␈α∞bounding␈α∞ordinal␈α∞to␈α∞be␈α
␈↓πw␈↓␈↓#
␈↓π␈↓#
w␈↓␈↓#
␈↓#
␈↓ ↓H␈↓then␈α
the␈α
SAMEFRINGE␈α
theorem␈α
for␈α
the␈α
above␈α
version␈α
of␈α
␈↓↓samefringe␈↓␈α
can␈α
be␈α
proved␈α
using␈α
the
␈↓ ↓H␈↓predicate
␈↓ ↓H␈↓␈↓ β
␈↓↓␈↓πF␈↓↓ n ≡ ∀x y: [[size x +size y]␈↓πw␈↓↓ + size ␈↓αa|␈↓↓x + size ␈↓αa|␈↓↓y = n ⊃ THM[x, y]]␈↓
␈↓ ↓H␈↓(Note␈α∩that␈α∩THM2␈α⊃will␈α∩need␈α∩to␈α⊃be␈α∩modified␈α∩to␈α⊃account␈α∩for␈α∩the␈α⊃new␈α∩form␈α∩of␈α∩␈↓↓samefringe.)␈α⊃ ␈↓
␈↓ ↓H␈↓Alternately one could axiomatize the lexicographic ordering of triples of numbers by
␈↓ ↓H␈↓␈↓ β0␈↓↓∀l␈↓β1␈↓↓ m␈↓β1␈↓↓ n␈↓β1␈↓↓ l m n: [(l␈↓β1␈↓↓, m␈↓β1␈↓↓, n␈↓β1␈↓↓) < (l, m, n) ≡ ␈↓
␈↓ ↓H␈↓␈↓ β]␈↓↓[l␈↓β1␈↓↓ < l] ∨ [l␈↓β1␈↓↓ = l ∧ m␈↓β1␈↓↓ < m] ∨ [l␈↓β1␈↓↓ = l ∧ m␈↓β1␈↓↓ = m ∧ n␈↓β1␈↓↓ < n]]␈↓
␈↓ ↓H␈↓This␈α∞ordering␈α∞is␈α
well-founded␈α∞(has␈α∞no␈α∞infinite␈α
decreasing␈α∞sequences)␈α∞and␈α
so␈α∞we␈α∞have␈α∞a␈α
schema
␈↓ ↓H␈↓analogous to ␈↓¬NUMBINDUCTION ␈↓given by
␈↓ ↓H␈↓␈↓ α]␈↓↓∀l m n: [∀l␈↓β1␈↓↓ m␈↓β1␈↓↓ n␈↓β1␈↓↓: [(l␈↓β1␈↓↓, m␈↓β1␈↓↓, n␈↓β1␈↓↓) < (l, m, n) ⊃␈↓πF␈↓↓(l␈↓β1␈↓↓, m␈↓β1␈↓↓, n␈↓β1␈↓↓)] ⊃ ␈↓πF␈↓↓(l, m, n)]␈↓
␈↓ ↓H␈↓␈↓ ¬A␈↓↓⊃ ∀l m n: ␈↓πF␈↓↓(l, m, n)␈↓
␈↓ ↓H␈↓The SAMEFRINGE theorem can now be proved using this schema with the predicate
␈↓ ↓H␈↓␈↓ αD␈↓↓␈↓πF␈↓↓(l, m, n) ≡ ∀x y: [l = size x +size y ∧ m = size ␈↓αa|␈↓↓y ∧ n = size ␈↓αa|␈↓↓x ⊃ THM[x, y]]␈↓
␈↓ ↓H␈↓10. ␈↓αFunctionals and Least Fixedpoints. ␈↓
␈↓ ↓H␈↓ Now␈α⊂we␈α⊂will␈α⊂describe␈α⊂a␈α⊂method␈α⊂for␈α⊂determining␈α⊂the␈α⊂function␈α⊂computed␈α⊂by␈α⊃a␈α⊂recursive
␈↓ ↓H␈↓program.␈α∂ This␈α∂will␈α⊂allow␈α∂us␈α∂to␈α⊂justify␈α∂the␈α∂use␈α⊂of␈α∂the␈α∂functional␈α⊂equation␈α∂to␈α∂represent␈α⊂a␈α∂total
␈↓ ↓H␈↓function.␈α
It␈α
will␈α
also␈α
provide␈α
a␈α
means␈α
of␈α
justifying␈α
the␈α
use␈α
of␈α
the␈α
functional␈α
equation␈α
together
␈↓ ↓H␈↓with␈α∪the␈α∪minimization␈α∪schema␈α∪to␈α∪represent␈α∩partial␈α∪functions␈α∪in␈α∪general.␈α∪ The␈α∪latter␈α∪will␈α∩be
␈↓ ↓H␈↓discussed in a later section.
␈↓ ↓H␈↓ We are interested in recursive programs of the form
␈↓ ↓H␈↓␈↓ ¬-␈↓↓f x ← ␈↓πt␈↓↓[f] x ␈↓
␈↓ ↓H␈↓where␈α∪␈↓πt␈↓␈α∪is␈α∪a␈α∪computable␈α∪functional.␈α∪ A␈α∪functional␈α∪is␈α∪a␈α∪higher␈α∪order␈α∪function␈α∪which␈α∩takes
␈↓ ↓H␈↓functions␈α
as␈α
arguments␈α
as␈α
well␈α
as␈α
ordinary␈α
arguments.␈α
The␈α
result␈α
of␈α
applying␈α
a␈α
functional␈α
to␈α
a
␈↓ ↓H␈↓72␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓function␈αis␈αa␈αfunction␈αwhich␈αis␈αthen␈αapplied␈αto␈αthe␈αrest␈αof␈αthe␈αarguments.␈α For␈αexample␈αthe␈αLISP
␈↓ ↓H␈↓program␈α␈↓↓mapcar␈↓␈αdefines␈αa␈αfunctional.␈α We␈αwill␈αconsider␈αfunctiionals␈α constructed␈αusing␈αa␈αfunction
␈↓ ↓H␈↓variable␈α
and␈α
a␈α∞set␈α
of␈α
base␈α∞functions␈α
and␈α
predicates␈α
in␈α∞a␈α
manner␈α
similar␈α∞to␈α
the␈α
formation␈α∞of␈α
λ-
␈↓ ↓H␈↓expressions␈α∂(but␈α∂omitting␈α⊂quantifiers).␈α∂ Functionals␈α∂constructed␈α∂in␈α⊂this␈α∂manner␈α∂are␈α⊂called␈α∂␈↓↓term␈↓
␈↓ ↓H␈↓␈↓↓functionals.␈αIn␈↓␈αthe␈αcase␈αof␈αLISP␈αprograms,␈αthe␈αnon-logical␈αfunctions␈αand␈αpredicates␈αare␈α␈↓↓car,␈↓␈α␈↓↓cdr,␈↓
␈↓ ↓H␈↓␈↓↓cons,␈↓ ␈↓↓atom␈↓ and ␈↓↓null.␈↓
␈↓ ↓H␈↓ For example, for the function ␈↓↓append␈↓ the functional is
␈↓ ↓H␈↓␈↓ ∧≠␈↓↓␈↓πt␈↓↓ = λF: λx y: [␈↓αif␈↓↓ ␈↓αn|␈↓↓x ␈↓αthen␈↓↓ y ␈↓αelse␈↓↓ ␈↓αa|␈↓↓x . F[␈↓αd|␈↓↓x,y]]␈↓
␈↓ ↓H␈↓or, using infix notation,
␈↓ ↓H␈↓␈↓ ∧α␈↓↓␈↓πt␈↓↓ = λOP: λx y: [␈↓αif␈↓↓ ␈↓αn|␈↓↓x ␈↓αthen␈↓↓ y ␈↓αelse␈↓↓ ␈↓αa|␈↓↓x . [␈↓αd|␈↓↓x OP y]]␈↓.
␈↓ ↓H␈↓ In␈αorder␈αto␈αsay␈αwhat␈αfunction␈αis␈αcomputed␈αby␈αa␈αrecursive␈αprogram␈αof␈αthe␈αform␈αgiven␈αabove
␈↓ ↓H␈↓we␈α
will␈αneed␈α
to␈αintroduce␈α
the␈αconcepts␈α
of␈α
␈↓↓partial␈↓␈α␈↓↓ordering␈↓␈α
and␈αof␈α
␈↓↓least␈↓␈α␈↓↓fixedpoints.␈↓␈α
We␈αgive␈α
here
␈↓ ↓H␈↓only a brief summary. A more complete discussion can be found in (Manna 1974, Chapter 5).
␈↓ ↓H␈↓ Given␈αa␈αdomain␈αD,␈αwe␈αform␈αa␈αdomain␈αD␈↓∧+␈↓␈αby␈αadjoining␈αthe␈αundefined␈αelement␈α␈↓π|␈↓␈αto␈αD.␈α We
␈↓ ↓H␈↓use␈αupper␈α
case␈αletters␈α
to␈αrange␈α
over␈αD␈↓∧+␈↓␈αwhere␈α
the␈αcorresponding␈α
lower␈αcase␈α
letters␈αrange␈α
over␈αD.
␈↓ ↓H␈↓A partial ordering, ␈↓π≤␈↓ (read "less defined than or equal"), on D␈↓∧+␈↓ is defined by
␈↓ ↓H␈↓␈↓ ¬&␈↓↓X ␈↓π≤␈↓↓ Y ≡ X = ␈↓π|␈↓↓ ∨ X = Y ␈↓.
␈↓ ↓H␈↓(Recall␈αthat␈αa␈αpartial␈α
ordering␈αon␈αa␈αset␈αS␈α
is␈αa␈αrelation␈αon␈α(S␈α
␈↓¬x␈α␈↓S)␈αthat␈αis␈αtransitive,␈α
reflexive␈αand
␈↓ ↓H␈↓antisymmetric.)␈α⊂ For␈α⊂example␈α⊃in␈α⊂the␈α⊂domain␈α⊃SE␈↓∧+␈↓␈α⊂we␈α⊂have␈α⊃ ␈↓π|␈↓ ␈↓π≤␈↓ ␈↓¬NIL␈↓␈α⊂ and␈α⊂ ␈↓¬NIL␈↓ ␈↓π≤␈↓ ␈↓¬NIL␈↓,␈α⊃ but␈α⊂not
␈↓ ↓H␈↓␈↓¬NIL␈↓ ␈↓π≤␈↓ (␈↓¬NIL␈↓ . ␈↓¬NIL␈↓).␈α∂ This␈α∂partial␈α⊂ordering␈α∂(sometimes␈α∂called␈α∂the␈α⊂"flat"␈α∂partial␈α∂ordering)␈α⊂is␈α∂indeed
␈↓ ↓H␈↓trivial.␈α A␈αpartial␈αordering␈αcan␈αbe␈αrepresented␈α
by␈αa␈αdiagram␈α in␈αwhich␈αall␈αelements␈αless␈α
than␈αbut
␈↓ ↓H␈↓not␈αequal␈αto␈αa␈αgiven␈αelement␈αare␈α"below"␈αthat␈αelement.␈α For␈αour␈αtrivial␈αordering␈αthe␈α
diagram␈αhas
␈↓ ↓H␈↓only␈αtwo␈α
layers:␈αthe␈α
bottom␈αlayer␈α
consisting␈αof␈α
the␈αsingle␈α
element␈α␈↓π|␈↓␈α
and␈αa␈α
top␈αlayer␈α
consisting␈αof
␈↓ ↓H␈↓the␈αrest.␈α Hence␈αthe␈αname␈α
"flat".␈α Despite␈αthe␈αtrivial␈αnature␈α
of␈αthis␈αparticular␈αpartial␈αordering,␈α
the
␈↓ ↓H␈↓concept turns out to be very useful.
␈↓ ↓H␈↓ Given␈α∞domains␈α∞D1␈α∞and␈α∞D2␈α∞and␈α∞a␈α∞partial␈α∞function␈α∞␈↓↓f:␈↓␈α∞D1→D2,␈α∞we␈α∞extend␈α∞␈↓↓f␈↓␈α∞to␈α∂a␈α∞ function
␈↓ ↓H␈↓from␈αD1␈↓∧+␈↓␈αto␈α
D2␈↓∧+␈↓␈αby␈αdefining␈α
␈↓↓f ␈↓π|␈↓↓ = ␈↓π|␈↓↓␈↓␈α and,␈αif␈α
␈↓↓f␈↓␈αis␈αundefined␈α
for␈α␈↓↓x␈↓␈αin␈α
D1,␈αthen␈α ␈↓↓f x = ␈↓π|␈↓↓␈↓.␈α(Note␈α
that
␈↓ ↓H␈↓we␈αuse␈αthe␈αsame␈αsymbol␈αto␈αdenote␈αthe␈αundefined␈αelement␈αregardless␈αof␈αdomain.␈αWe␈αshall␈αalso␈αuse
␈↓ ↓H␈↓this␈α⊂symbol␈α⊂to␈α⊂denote␈α⊂a␈α⊂totally␈α⊂undefined␈α⊂function.␈α⊂ Context␈α⊂will␈α⊂make␈α⊂it␈α⊂clear␈α⊂what␈α⊃is␈α⊂being
␈↓ ↓H␈↓denoted␈α∩by␈α∩␈↓π|␈↓.)␈α∩ Other␈α∩extensions␈α∩are␈α∩possible␈α∩but␈α∩we␈α∩will␈α∩not␈α∩consider␈α∩them.␈α∩ We␈α∪will␈α∩not
␈↓ ↓H␈↓distinguish␈α
between␈α∞a␈α
function␈α
and␈α∞its␈α
extension␈α
as␈α∞we␈α
are␈α
considering␈α∞only␈α
one␈α∞extension.␈α
We
␈↓ ↓H␈↓"lift"␈α∞the␈α∂partial␈α∞orderings␈α∞on␈α∂D1␈↓∧+␈↓␈α∞and␈α∂D2␈↓∧+␈↓␈α∞to␈α∞the␈α∂set␈α∞of␈α∂partial␈α∞functions␈α∞from␈α∂D1␈↓∧+␈↓␈α∞to␈α∂D2␈↓∧+␈↓␈α∞by
␈↓ ↓H␈↓defining
␈↓ ↓H␈↓␈↓ ¬∩␈↓↓f ␈↓π≤␈↓↓ g ≡ ∀x in D1: f x ␈↓π≤␈↓↓ g x ␈↓.
␈↓ ↓H␈↓For example if D1 and D2 are the domain SE,
␈↓ ↓H␈↓␈↓ ¬ε␈↓↓f␈↓β1␈↓↓ = λx: [␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ ␈↓π|␈↓↓]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *73
␈↓ ↓H␈↓␈↓ βs␈↓↓f␈↓β2␈↓↓ = λx: [␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓x ␈↓αthen␈↓↓ ␈↓αa|␈↓↓x ␈↓αelse␈↓↓ ␈↓π|␈↓↓] ␈↓.
␈↓ ↓H␈↓then ␈↓↓f␈↓β1␈↓↓ ␈↓π≤␈↓↓ f␈↓β2␈↓↓ ␈↓.
␈↓ ↓H␈↓ The␈α∞partial␈α∞ordering␈α∞on␈α∞the␈α∞function␈α∞domains␈α∞is␈α∞not␈α∞as␈α∞trivial␈α∞as␈α∞that␈α∞on␈α∞the␈α∞underlying
␈↓ ↓H␈↓domains␈α
but␈α
it␈α
is␈α
still␈α
very␈α∞easy␈α
to␈α
work␈α
with.␈α
A␈α
useful␈α∞concept␈α
here␈α
it␈α
that␈α
of␈α
␈↓↓lub␈↓␈α∞(least␈α
upper
␈↓ ↓H␈↓bound␈αto␈αbe␈αprecise).␈α ␈↓↓lub␈↓␈αacts␈αon␈αsets␈αof␈αfunctions␈αand␈αproduces␈αthe␈αleast␈αfunction␈αin␈αthe␈αdomain
␈↓ ↓H␈↓that␈α
contains␈α
(or␈α
is␈α∞equal␈α
to)␈α
every␈α
function␈α∞in␈α
the␈α
set,␈α
if␈α
such␈α∞a␈α
function␈α
exists.␈α
A␈α∞special␈α
case
␈↓ ↓H␈↓where ␈↓↓lub␈↓ is always defined is the following. Let ␈↓↓f␈↓␈↓βi␈↓ be a set of functions satisfying
␈↓ ↓H␈↓␈↓ ¬\␈↓↓f␈↓βi␈↓↓ ␈↓π≤␈↓↓ f␈↓βi+1␈↓↓␈↓ for all ␈↓↓i.␈↓
␈↓ ↓H␈↓Such␈α∞a␈α
set␈α∞is␈α∞called␈α
a␈α∞␈↓↓chain.␈↓␈α
The␈α∞␈↓↓lub␈↓␈α∞of␈α
a␈α∞chain␈α
always␈α∞exists.␈α∞ Call␈α
the␈α∞resulting␈α∞function␈α
␈↓↓f,␈↓
␈↓ ↓H␈↓then␈αwe␈αcan␈αcompute␈α
␈↓↓f␈↓␈αas␈αfollows.␈α If␈α
for␈αsome␈α␈↓↓n␈↓␈α␈↓↓f␈↓βn␈↓↓[x] ≠ ␈↓π|␈↓↓␈↓␈αthen␈α
␈↓↓f[x] = f␈↓βn␈↓↓[x]␈↓.␈α If␈αno␈αsuch␈α
␈↓↓n␈↓␈αexists
␈↓ ↓H␈↓then ␈↓↓f[x] = ␈↓π|␈↓↓␈↓. This works because of the simple nature of the partial ordering.
␈↓ ↓H␈↓ If␈α␈↓πt␈↓␈αis␈αa␈αfunctional␈αmapping␈αfunctons␈αfrom␈αD1␈αto␈αD2␈αto␈αfunctions␈αof␈αthe␈αsame␈αtype␈αthen␈αa
␈↓ ↓H␈↓function␈α∞␈↓↓f␈↓␈α∞satisfying␈α∞␈↓↓f = ␈↓πt␈↓↓[f]␈↓␈α∞is␈α
a␈α∞␈↓↓fixedpoint␈↓␈α∞of␈α∞␈↓πt␈↓.␈α∞ If␈α
furthermore␈α∞for␈α∞any␈α∞other␈α∞function␈α∞␈↓↓g␈↓␈α
we
␈↓ ↓H␈↓have␈α
␈↓↓g = ␈↓πt␈↓↓[g] ⊃ f ␈↓π≤␈↓↓ g␈↓␈α
then␈α␈↓↓f␈↓␈α
is␈α
the␈α
␈↓↓least␈↓␈α␈↓↓fixedpoint␈↓␈α
of␈α
␈↓πt␈↓.␈α
For␈αour␈α
restricted␈α
class␈α
of␈αfunctionals
␈↓ ↓H␈↓the least fixedpoint always exists. We denote the least fixedpoint of ␈↓πt␈↓ by ␈↓αY␈↓[␈↓πt␈↓].
␈↓ ↓H␈↓ The␈α∩existence␈α∩of␈α⊃␈↓αY␈↓[␈↓πt␈↓]␈α∩is␈α∩due␈α⊃to␈α∩the␈α∩fact␈α⊃that␈α∩our␈α∩functionals␈α⊃have␈α∩some␈α∩very␈α⊃special
␈↓ ↓H␈↓properties.␈α
One␈α∞such␈α
property␈α∞is␈α
␈↓↓monotonicity.␈↓␈α∞ A␈α
functional,␈α∞␈↓πt␈↓␈α
that␈α∞is␈α
monotonic␈α∞preserves␈α
the
␈↓ ↓H␈↓partial ordering of its function domain. In particular, for functions ␈↓↓f␈↓ and ␈↓↓g,␈↓ ␈↓πt␈↓ must satisfy
␈↓ ↓H␈↓␈↓ ¬ε␈↓↓f ␈↓π≤␈↓↓ g ⊃ ␈↓πt␈↓↓[f] ␈↓π≤␈↓↓ ␈↓πt␈↓↓[g] . ␈↓
␈↓ ↓H␈↓ A␈α
second␈α
propery␈α∞is␈α
␈↓↓continuity.␈↓␈α
A␈α
continuous␈α∞functional␈α
is␈α
monotonic␈α
and␈α∞must␈α
preserve
␈↓ ↓H␈↓␈↓↓lub␈↓'s␈α∞of␈α∞chains␈α∞of␈α∞functions.␈α∞ In␈α∞particular␈α∞if␈α∞␈↓πt␈↓␈α∞is␈α∞a␈α∞continuous␈α∞functional␈α∞and␈α∞␈↓↓f␈↓βi␈↓↓␈↓␈α∞is␈α∞a␈α∂chain␈α∞of
␈↓ ↓H␈↓functions then the set ␈↓↓␈↓πt␈↓↓[f␈↓βi␈↓↓]␈↓ of functions is also a chain and
␈↓ ↓H␈↓␈↓ ¬A␈↓↓␈↓πt␈↓↓[lub{f␈↓βi␈↓↓}] = lub{␈↓πt␈↓↓[f␈↓βi␈↓↓]}␈↓.
␈↓ ↓H␈↓ Now we show how to construct ␈↓αY␈↓[␈↓πt␈↓] for a continuous functional ␈↓πt␈↓. Let
␈↓ ↓H␈↓␈↓ ¬3␈↓↓f␈↓β0␈↓↓ = ␈↓π|␈↓↓␈↓ and ␈↓↓f␈↓βi+1␈↓↓ = ␈↓πt␈↓↓[f␈↓βi␈↓↓] ␈↓.
␈↓ ↓H␈↓Then
␈↓ ↓H␈↓␈↓ ¬N␈↓↓␈↓π|␈↓↓ = f␈↓β0␈↓↓ ␈↓π≤␈↓↓ f␈↓β1␈↓↓ = ␈↓πt␈↓↓[f␈↓β0␈↓↓]␈↓
␈↓ ↓H␈↓and, since ␈↓πt␈↓ is monotonic,
␈↓ ↓H␈↓␈↓ ¬\␈↓↓f␈↓βi␈↓↓ ␈↓π≤␈↓↓ f␈↓βi+1␈↓↓␈↓ for all ␈↓↓i.␈↓
␈↓ ↓H␈↓Thus␈α
the␈α
functions␈α
␈↓↓f␈↓βi␈↓↓␈↓␈α
form␈α
a␈α
chain,␈α
␈↓↓lub{f␈↓βi␈↓↓}␈↓␈α
is␈α
defined,␈α
and␈α
␈↓↓␈↓αY␈↓↓[␈↓πt␈↓↓] = lub{f␈↓βi␈↓↓}␈↓␈α
.␈α
Intuitively␈α
one␈αcan
␈↓ ↓H␈↓think␈αof␈αthe␈α␈↓↓f␈↓βi␈↓↓␈↓'s␈αas␈αforming␈αa␈αsequence␈αof␈αsuccessively␈αbetter␈αapproximations␈αto␈αthe␈αfunction␈α␈↓αY␈↓[␈↓πt␈↓].
␈↓ ↓H␈↓74␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓What␈αis␈αbeing␈αapproximated␈αis␈αthe␈αdomain␈α
of␈αdefinition.␈α Repeated␈αapplications␈αof␈α␈↓πt␈↓␈αincrease␈α
the
␈↓ ↓H␈↓domain of definition (unless, of course, we have for some ␈↓↓i␈↓ that ␈↓↓f␈↓βi␈↓↓␈↓ = ␈↓αY␈↓[␈↓πt␈↓]).
␈↓ ↓H␈↓ For example if
␈↓ ↓H␈↓␈↓ ∧W␈↓↓␈↓πt␈↓↓ = λf: λx: [␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ f[␈↓αa|␈↓↓x]] ␈↓
␈↓ ↓H␈↓then, for ␈↓↓f␈↓β1␈↓↓␈↓ and ␈↓↓f␈↓β2␈↓↓␈↓ as defined above, we have
␈↓ ↓H␈↓␈↓ ¬$␈↓↓f␈↓β1␈↓↓ = ␈↓πt␈↓↓[␈↓π|␈↓↓]␈↓ and ␈↓↓f␈↓β2␈↓↓ = ␈↓πt␈↓↓[f␈↓β1␈↓↓] ␈↓.
␈↓ ↓H␈↓The␈α⊂␈↓↓n␈↓␈α∂mentioned␈α⊂above␈α⊂in␈α∂the␈α⊂description␈α⊂of␈α∂how␈α⊂to␈α⊂compute␈α∂␈↓↓lub␈↓␈α⊂of␈α⊂a␈α∂chain␈α⊂of␈α⊂functions␈α∂is
␈↓ ↓H␈↓essentially the number of ␈↓↓car␈↓'s that must be applied to ␈↓↓x␈↓ to reach an atom.
␈↓ ↓H␈↓[A␈α→parenthetical␈α_remark:␈α→ the␈α_notions␈α→of␈α_␈↓↓monotonicity␈↓␈α→and␈α_␈↓↓continuity␈↓␈α→defined␈α→above␈α_are
␈↓ ↓H␈↓generalizations␈α∪of␈α∪those␈α∪encountered␈α∪in␈α∪a␈α∪calculus␈α∪or␈α∪analysis␈α∪course␈α∪in␈α∀mathematics.␈α∪ Such
␈↓ ↓H␈↓generalizations␈αare␈αcommon␈αin␈αthe␈αstudy␈αof␈αtopology␈αand␈αthe␈αabove␈αarguments␈αcan␈αbe␈αformulated
␈↓ ↓H␈↓in a topological framework.]
␈↓ ↓H␈↓ Now␈α∞we␈α∞are␈α∞ready␈α∞to␈α∞define␈α∞the␈α∞function␈α∞computed␈α∞by␈α∞a␈α∞recursive␈α∞program␈α∞and␈α∞to␈α∞show
␈↓ ↓H␈↓how␈α
this␈α∞function␈α
is␈α∞characterized␈α
by␈α∞the␈α
corresponding␈α
functional␈α∞equation␈α
in␈α∞the␈α
case␈α∞of␈α
total
␈↓ ↓H␈↓functions.␈α∂ If␈α∂␈↓πt␈↓␈α∂is␈α∞a␈α∂term functional␈α∂then␈α∂the␈α∂function,␈α∞␈↓↓f,␈↓␈α∂computed␈α∂by␈α∂the␈α∂recursive␈α∞program,
␈↓ ↓H␈↓␈↓↓f x ← ␈↓πt␈↓↓[f] x␈↓ is ␈↓αY␈↓[␈↓πt␈↓]. Thus ␈↓↓f␈↓ satisfies the functional equation
␈↓ ↓H␈↓␈↓ ¬↓␈↓↓∀x: [f x = ␈↓πt␈↓↓[f] x] . ␈↓
␈↓ ↓H␈↓If ␈↓↓f␈↓ is total then ␈↓↓f x ≠ ␈↓π|␈↓↓␈↓ for any ␈↓↓x␈↓ in D1. This means that for any function ␈↓↓g␈↓
␈↓ ↓H␈↓␈↓ ¬∩␈↓↓f ␈↓π≤␈↓↓ g␈↓ iff ␈↓↓f = g . ␈↓
␈↓ ↓H␈↓Thus ␈↓↓f␈↓ is the unique function satisfying the functional equation.
␈↓ ↓H␈↓ In␈α∂the␈α∂case␈α∂that␈α∂␈↓↓f␈↓␈α∂is␈α∂not␈α∂total␈α∂the␈α∂functional␈α∂equation␈α∂is␈α∂not␈α∂sufficient.␈α∂We␈α∂also␈α⊂need␈α∂to
␈↓ ↓H␈↓express␈α∞the␈α
fact␈α∞that␈α
␈↓↓f␈↓␈α∞is␈α∞the␈α
least␈α∞function␈α
satisfying␈α∞this␈α
equation.␈α∞ The␈α∞minimization␈α
schema
␈↓ ↓H␈↓will do that.
␈↓ ↓H␈↓11. ␈↓αThe Minimization Schema.␈↓
␈↓ ↓H␈↓ As␈α∞mentioned␈α∞before,␈α∂the␈α∞functional␈α∞equation␈α∞of␈α∂a␈α∞program␈α∞doesn't␈α∞in␈α∂general␈α∞completely
␈↓ ↓H␈↓characterize it. For example, the program
␈↓ ↓H␈↓␈↓ ¬'␈↓↓f1 x ← f1 x ␈↓
␈↓ ↓H␈↓leads to the sentence
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *75
␈↓ ↓H␈↓␈↓ ¬$␈↓↓∀x: [f1 x = f1 x] ␈↓
␈↓ ↓H␈↓which␈α⊂provides␈α⊂no␈α⊂information␈α⊂although␈α∂the␈α⊂function␈α⊂␈↓↓f1␈↓␈α⊂is␈α⊂undefined␈α∂for␈α⊂all␈α⊂␈↓↓x.␈↓␈α⊂ This␈α⊂is␈α∂not
␈↓ ↓H␈↓always the case, since the program
␈↓ ↓H␈↓␈↓ ¬'␈↓↓f2 x ← [f2 x] . ␈↓¬NIL␈↓↓ ␈↓
␈↓ ↓H␈↓has the functional equation
␈↓ ↓H␈↓␈↓ ¬#␈↓↓∀x: [f2 x = [f2 x] . ␈↓¬NIL␈↓↓] . ␈↓
␈↓ ↓H␈↓from which ␈↓↓∀x: [¬issexp f2 x]␈↓ can be proved by induction.
␈↓ ↓H␈↓ In␈α⊃order␈α⊂to␈α⊃characterize␈α⊃recursive␈α⊂programs,␈α⊃we␈α⊃need␈α⊂some␈α⊃way␈α⊃of␈α⊂asking␈α⊃for␈α⊃the␈α⊂least
␈↓ ↓H␈↓defined␈αsolution␈αof␈αthe␈αfunctional␈αequation.␈α That␈αis␈αwe␈αwant␈αthe␈αleast␈αfixedpoint␈αof␈αthe␈αdefining
␈↓ ↓H␈↓functional.
␈↓ ↓H␈↓ Consider the recursive program
␈↓ ↓H␈↓␈↓ ¬-␈↓↓f x ← ␈↓πt␈↓↓[f] x ␈↓
␈↓ ↓H␈↓which has the functional equation
␈↓ ↓H␈↓␈↓ ¬)␈↓↓∀x: [f x = ␈↓πt␈↓↓[f] x] . ␈↓
␈↓ ↓H␈↓It␈αcan␈αbe␈α
shown␈αthat␈αrequiring␈α
a␈αfixedpoint␈α␈↓↓f␈↓␈α
of␈αa␈αfunctional␈α
␈↓πt␈↓␈αto␈αbe␈α
minimal␈αis␈αequivalent␈αto␈α
the
␈↓ ↓H␈↓␈↓↓minimization schema␈↓
␈↓ ↓H␈↓␈↓ ∧`␈↓↓∀x: [␈↓πt␈↓↓[F] x ␈↓π≤␈↓↓ F x] ⊃ ∀x: [f x ␈↓π≤␈↓↓ F x]␈↓
␈↓ ↓H␈↓where ␈↓↓F␈↓ is a function variable.
␈↓ ↓H␈↓ The␈α∞partial␈α∞ordering␈α∞can␈α∞be␈α∞removed␈α∂from␈α∞the␈α∞statement␈α∞of␈α∞the␈α∞minimization␈α∂schema␈α∞by
␈↓ ↓H␈↓noting that for functions ␈↓↓f␈↓ and ␈↓↓g␈↓ we have
␈↓ ↓H␈↓␈↓ ∧{␈↓↓f ␈↓π≤␈↓↓ g ≡ ∀x: [isD f x ⊃ f x = g x]␈↓
␈↓ ↓H␈↓where␈αD␈αis␈αthe␈αdomain␈αcontaining␈αthe␈αrange␈αof␈α␈↓↓f␈↓␈αand␈αthe␈αpredicate␈α␈↓↓isD␈↓␈αstates␈αthat␈αits␈αargument␈αis
␈↓ ↓H␈↓in the domain D. Thus ␈↓↓isD f x␈↓ means that ␈↓↓f x ≠ ␈↓π|␈↓↓␈↓. The minimization schema then becomes
␈↓ ↓H␈↓␈↓ βB␈↓↓∀x: [isD ␈↓πt␈↓↓[F] x ⊃ F x = ␈↓πt␈↓↓[F] x] ⊃ ∀x: [isD f x ⊃ f x = F x] ␈↓.
␈↓ ↓H␈↓ The␈α∂simplest␈α∞application␈α∂of␈α∂the␈α∞schema␈α∂is␈α∞to␈α∂show␈α∂that␈α∞the␈α∂program␈α∞for␈α∂␈↓↓f1␈↓␈α∂given␈α∞above
␈↓ ↓H␈↓computes the totally undefined function. The schema becomes
␈↓ ↓H␈↓␈↓ ∧d␈↓↓∀x: [F x ␈↓π≤␈↓↓ F x] ⊃ ∀x: [f1 x ␈↓π≤␈↓↓ F x] ␈↓.
␈↓ ↓H␈↓The␈α⊂left␈α⊂side␈α∂of␈α⊂the␈α⊂implication␈α⊂is␈α∂identically␈α⊂true.␈α⊂ Taking␈α⊂F[x]␈α∂=␈α⊂␈↓π|␈↓,␈α⊂ and␈α⊂remembering␈α∂that
␈↓ ↓H␈↓␈↓↓X ␈↓π≤␈↓↓ ␈↓π|␈↓↓␈↓ only if ␈↓↓X = ␈↓π|␈↓↓␈↓, the right side tells us that ␈↓↓f1 x = ␈↓π|␈↓↓␈↓.
␈↓ ↓H␈↓76␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓ The␈α⊃minimization␈α⊃schema␈α∩provides␈α⊃an␈α⊃alternate␈α⊃method␈α∩for␈α⊃proving␈α⊃the␈α∩correctness␈α⊃of
␈↓ ↓H␈↓␈↓↓samefringe.␈↓␈α
To␈α
simplify␈α
matters␈α
we␈α
eliminate␈αthe␈α
auxilliary␈α
function␈α
␈↓↓samea␈↓␈α
from␈α
the␈αdefinition␈α
of
␈↓ ↓H␈↓␈↓↓samefringea␈↓␈α∂thus␈α∞eliminating␈α∂the␈α∞problem␈α∂of␈α∞having␈α∂to␈α∞deal␈α∂with␈α∞mutually␈α∂recursive␈α∞programs.
␈↓ ↓H␈↓The functional defining ␈↓↓samefringea␈↓ now becomes
␈↓ ↓H␈↓␈↓ α≠␈↓↓␈↓πt␈↓↓ = λF: λx y: [[x eeq y] oor [[nnot aatom x aand nnot aatom y] aand ␈↓
␈↓ ↓H␈↓␈↓ αu␈↓↓ [[␈↓αa|␈↓↓gopher x eeq ␈↓αa|␈↓↓gopher y] aand F[␈↓αd|␈↓↓gopher x, ␈↓αd|␈↓↓gopher y]]]] ␈↓.
␈↓ ↓H␈↓Now␈αwe␈α
form␈αan␈α
axiom␈αschema␈α
from␈αthe␈αminimization␈α
schema␈αby␈α
using␈αthe␈α
␈↓πt␈↓␈αgiven␈α
above␈αand
␈↓ ↓H␈↓␈↓↓samefringea␈↓ for ␈↓↓f.␈↓ We instantiate this schema with
␈↓ ↓H␈↓␈↓ ∧;␈↓↓F[x, y] = fringe x eeq fringe y . ␈↓
␈↓ ↓H␈↓The proof then consists of the following steps
␈↓ ↓H␈↓ 1) ␈↓↓∀x y: [istv ␈↓πt␈↓↓[F] [x, y]]␈↓
␈↓ ↓H␈↓ 2) ␈↓↓∀x y: [F[x, y] = ␈↓πt␈↓↓[F] [x, y]]␈↓
␈↓ ↓H␈↓ 3) ␈↓↓∀x y: istv samefringea[x, y]␈↓
␈↓ ↓H␈↓from 1) - 3) and the axiom instantiation we conclude
␈↓ ↓H␈↓ 4) ␈↓↓∀x y: [fringe x eeq fringe y = samefringea[x, y]]␈↓
␈↓ ↓H␈↓from␈α3)␈αwe␈αknow␈αthat␈α␈↓↓samefringea[x, y]␈↓␈αis␈αeither␈α␈↓¬TT␈α␈↓or␈α␈↓¬FF␈α␈↓and␈αso␈αby␈αa␈αsimple␈αcase␈αanalysis␈αusing
␈↓ ↓H␈↓the TV-lemmas, the fact that ␈↓¬TT ␈↓≠ ␈↓¬FF, the ␈↓definition of ␈↓↓samefringe␈↓ and 4) we conclude
␈↓ ↓H␈↓ 5) ␈↓↓∀x y: [samefringe[x, y] ≡ fringe x = fringe y]␈↓
␈↓ ↓H␈↓as desired.
␈↓ ↓H␈↓ The␈αminimization␈αschema␈αcan␈αsometimes␈αbe␈αused␈αto␈αshow␈αpartial␈αcorrectness.␈α For␈αexample,
␈↓ ↓H␈↓the well known 91-function is defined by the recursive program over the integers
␈↓ ↓H␈↓␈↓ βq␈↓↓f91 x ← ␈↓αif␈↓↓ x > 100 ␈↓αthen␈↓↓ x - 10 ␈↓αelse␈↓↓ f91 f91 [x + 11] ␈↓.
␈↓ ↓H␈↓The goal is to show that
␈↓ ↓H␈↓␈↓ ∧⊃␈↓↓∀x: [f91 x = ␈↓αif␈↓↓ x > 100 ␈↓αthen␈↓↓ x - 10 ␈↓αelse␈↓↓ 91] . ␈↓
␈↓ ↓H␈↓We apply the minimization schema with
␈↓ ↓H␈↓␈↓ ∧∩␈↓↓F x = ␈↓αif␈↓↓ x > 100 ␈↓αthen␈↓↓ x - 10 ␈↓αelse␈↓↓ 91 , ␈↓
␈↓ ↓H␈↓and␈α
it␈αcan␈α
be␈α
shown␈αby␈α
an␈α
explicit␈αcalculation␈α
without␈α
induction␈αthat␈α
the␈α
premiss␈αof␈α
the␈αschema␈α
is
␈↓ ↓H␈↓satisfied, and this shows that ␈↓↓f91,␈↓ whenever defined has the desired value.
␈↓ ↓H␈↓ The␈α∂method␈α∂of␈α∂␈↓↓recursion␈↓␈α∂␈↓↓induction␈↓␈α∂is␈α∂also␈α∂an␈α∂immediate␈α∂application␈α∂of␈α∂the␈α∞minimization
␈↓ ↓H␈↓schema.␈α
If␈αwe␈α
show␈αthat␈α
two␈αfunctions␈α
satisfy␈αthe␈α
schema␈αof␈α
a␈αrecursive␈α
program,␈αwe␈α
show␈αthat
␈↓ ↓H␈↓they both equal the function computed by the program whereever the function is defined.
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *77
␈↓ ↓H␈↓ The␈α
utility␈α
of␈α
the␈α
minimization␈αschema␈α
for␈α
proving␈α
partial␈α
correctness␈α
or␈αnon-termination
␈↓ ↓H␈↓depends␈α∂on␈α∞our␈α∂ability␈α∞to␈α∂name␈α∂suitable␈α∞comparison␈α∂functions.␈α∞ ␈↓↓f1␈↓␈α∂and␈α∞␈↓↓f91␈↓␈α∂were␈α∂easily␈α∞treated,
␈↓ ↓H␈↓because␈α⊃the␈α⊃necessary␈α⊃comparison␈α⊃functions␈α⊃could␈α⊃be␈α⊃given␈α⊃explicitly␈α⊃without␈α⊃recursion.␈α⊂ Any
␈↓ ↓H␈↓extension␈αof␈αthe␈αlanguage␈αthat␈αprovides␈αnew␈αtools␈αfor␈αnaming␈αcomparison␈αfunctions,␈αe.g.␈αgoing␈αto
␈↓ ↓H␈↓higher order logic, will improve our ability to use the schema in proofs.
␈↓ ↓H␈↓ The␈α
minimization␈α
schema␈α
can␈α
be␈α
regarded␈α∞as␈α
an␈α
axiom␈α
schema␈α
involving␈α
a␈α∞second␈α
order
␈↓ ↓H␈↓function␈α
variable␈α
␈↓πt␈↓.␈α
What␈α
can␈α
be␈α
substituted␈α∞for␈α
␈↓πt␈↓␈α
is␈α
a␈α
quantifier␈α
free␈α
λ-expression␈α
in␈α∞a␈α
first
␈↓ ↓H␈↓order␈αfunction␈α
variable.␈α It␈αmay␈α
be␈αinteresting␈αto␈α
study␈αthe␈αsets␈α
of␈αfirst␈αorder␈α
sentences␈αthat␈αcan␈α
be
␈↓ ↓H␈↓generated␈α
by␈α
such␈αsecond␈α
order␈α
sentence␈αschemata.␈α
Presumably,␈α
sets␈αof␈α
sentences␈α
can␈αbe␈α
generated
␈↓ ↓H␈↓in this way that cannnot be generated by schemata with only first order function variables.
␈↓ ↓H␈↓α␈↓ εεExercises.
␈↓ ↓H␈↓α1. Simple structural induction proofs.
␈↓ ↓H␈↓Prove the following statements.
␈↓ ↓H␈↓ i) ␈↓↓∀u: u * ␈↓¬NIL␈↓↓ = u␈↓
␈↓ ↓H␈↓ ii) ␈↓↓∀u: islist reverse1 u␈↓
␈↓ ↓H␈↓ iii) ␈↓↓∀u: reverse u = reverse1 u␈↓
␈↓ ↓H␈↓ iv) ␈↓↓∀u: reverse reverse u = u␈↓
␈↓ ↓H␈↓ v) ␈↓↓∀u v: reverse[u * v] = [reverse v] * [reverse u]␈↓
␈↓ ↓H␈↓ vi) ␈↓↓∀x: flatten x = fringe x␈↓
␈↓ ↓H␈↓ vii) ␈↓↓∀x: (¬␈↓αat|␈↓↓x ⊃ size gopher x = size x)␈↓
␈↓ ↓H␈↓ viii) ␈↓↓∀x: (¬␈↓αat|␈↓↓x ⊃ ␈↓αat|␈↓↓␈↓αa|␈↓↓gopher x)␈↓
␈↓ ↓H␈↓where the necessary LISP function definitions are:
␈↓ ↓H␈↓ ␈↓↓u * v ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]␈↓
␈↓ ↓H␈↓ ␈↓↓reverse1 u ← if ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [reverse1 ␈↓αd|␈↓↓u] * <␈↓αa|␈↓↓u>␈↓
␈↓ ↓H␈↓ ␈↓↓reverse u ← rev[u, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓ ␈↓↓rev[u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ rev[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u . v]␈↓
␈↓ ↓H␈↓ ␈↓↓flatten x ← flat[x, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓ ␈↓↓flat[x, u] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ [x . u] ␈↓αelse␈↓↓ flat[␈↓αa|␈↓↓x, flat[␈↓αd|␈↓↓x, u]]␈↓
␈↓ ↓H␈↓ ␈↓↓fringe x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <x> ␈↓αelse␈↓↓ [fringe ␈↓αa|␈↓↓x] * [fringe ␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓78␈↓ ¬xChapter III␈↓ H
␈↓ ↓H␈↓ ␈↓↓gopher x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ gopher[␈↓αaa|␈↓↓x . [␈↓αda|␈↓↓x . ␈↓αd|␈↓↓x]]␈↓
␈↓ ↓H␈↓ ␈↓↓size x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x␈↓
␈↓ ↓H␈↓α2. Properties of substitutions and substitution lists.
␈↓ ↓H␈↓ With␈α∩function␈α∪definitions␈α∩as␈α∩given␈α∪below,␈α∩␈↓↓subst[x, y, z]␈↓␈α∩is␈α∪the␈α∩result␈α∩of␈α∪replacing␈α∩the
␈↓ ↓H␈↓variable␈α
␈↓↓y␈↓␈α
by␈α
the␈α
S-expression␈α
␈↓↓x␈↓␈α
whereever␈α
␈↓↓y␈↓␈α
occurs␈αin␈α
␈↓↓z.␈↓␈α
If␈α
␈↓↓s␈↓␈α
is␈α
a␈α
list␈α
of␈α
substitutions␈α
of␈αthe␈α
form
␈↓ ↓H␈↓␈↓↓y . x␈↓␈α
then␈α
␈↓↓sublis[z, s]␈↓␈αis␈α
the␈α
result␈α
of␈α"simultaneously"␈α
performing␈α
all␈αof␈α
the␈α
substitutions␈α
on␈αthe
␈↓ ↓H␈↓list␈α
to␈α␈↓↓z.␈↓␈α
If␈α
␈↓↓s1␈↓␈αand␈α
␈↓↓s2␈↓␈α
are␈αlists␈α
of␈α
substitutions␈αthen␈α
␈↓↓s1␈↓␈α
@␈α␈↓↓s2␈↓␈α
is␈α
"composition"␈α of␈α
the␈α
two.␈α Prove␈α
the
␈↓ ↓H␈↓following properties.
␈↓ ↓H␈↓ i) ␈↓↓∀x y z: subst[x, y, z] = sublis[z, <y . x>]␈↓
␈↓ ↓H␈↓ ii) ␈↓↓∀z s1 s2: sublis[z, s1 @ s2] = sublis[sublis[z, s1], s2]␈↓
␈↓ ↓H␈↓ iii) ␈↓↓∀z s1 s2 s3: sublis[z, s1 @ [s2 @ s3]] = sublis[z, [s1 @ s2] @ s3]␈↓
␈↓ ↓H␈↓ iv) ␈↓↓∀x y z: (¬occur[y, z] ⊃ subst[x, y, z] = z)␈↓
␈↓ ↓H␈↓ v) ␈↓↓∀x x1 y y1 z: ((y ≠ y1 ∧ ¬occur[y, x1]) ⊃ ␈↓
␈↓ ↓H␈↓ ␈↓↓subst[x1, y1, subst[x, y, z]] = subst[subst[x1, y1, x], y, subst[x1, y1, z]])␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓ ␈↓↓assoc[x, s] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓s ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ x = ␈↓αaa|␈↓↓s ␈↓αthen␈↓↓ ␈↓αa|␈↓↓s ␈↓αelse␈↓↓ assoc[x, ␈↓αd|␈↓↓s]␈↓
␈↓ ↓H␈↓ ␈↓↓subst[x, y, z] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓z ␈↓αthen␈↓↓ [␈↓αif␈↓↓ y = z ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ z] ␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ subst[x, y, ␈↓αa|␈↓↓z] . subst[x, y, ␈↓αd|␈↓↓z]␈↓
␈↓ ↓H␈↓ ␈↓↓occur[x, y] ← [x = y] ∨ [¬␈↓αat|␈↓↓y ∧ [occur[x, ␈↓αa|␈↓↓y] ∨ occur[x, ␈↓αd|␈↓↓y]]]␈↓.
␈↓ ↓H␈↓ ␈↓↓sublis[x, s] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ {assoc[x, s]}[λz: ␈↓αif␈↓↓ ␈↓αn|␈↓↓z ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ ␈↓αd|␈↓↓z]␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ sublis[␈↓αa|␈↓↓x, s] . sublis[␈↓αd|␈↓↓x, s]␈↓
␈↓ ↓H␈↓ ␈↓↓s1 @ s2 ← subsub[s1, s2] * s2␈↓
␈↓ ↓H␈↓ ␈↓↓subsub[s1, s2] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓s1 ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [␈↓αaa|␈↓↓s1 . sublis[␈↓αda|␈↓↓s1, s2]] . subsub[␈↓αd|␈↓↓s1, s2]␈↓
␈↓ ↓H␈↓α3. Pattern matching and unification properties.
␈↓ ↓H␈↓ (This is a fairly substantial exercise.)
␈↓ ↓H␈↓ With␈α⊂the␈α⊂further␈α∂function␈α⊂definitions␈α⊂given␈α⊂below,␈α∂␈↓↓inst[x, y, ␈↓¬NIL␈↓↓]␈↓␈α⊂is␈α⊂␈↓¬NO␈α∂␈↓if␈α⊂␈↓↓x␈↓␈α⊂is␈α⊂not␈α∂and
␈↓ ↓H␈↓instance␈αof␈αthe␈αpattern␈α␈↓↓y,␈↓␈αand␈αotherwise␈αis␈αthe␈αlist␈αof␈αsubstitions␈αwhich␈αwill␈αconvert␈α␈↓↓y␈↓␈αinto␈α␈↓↓x.␈↓␈α In
␈↓ ↓H␈↓the latter case we have ␈↓↓x = sublis[y, inst[x, y, ␈↓¬NIL␈↓↓]␈↓. Prove
␈↓ ↓H␈↓ i) ␈↓↓∀x y s: (inst[x, y, s] ≠ ␈↓¬NO ␈↓↓⊃ x = sublis[y, inst[x, y, s]])␈↓
␈↓ ↓H␈↓␈↓ ¬xChapter III␈↓ *79
␈↓ ↓H␈↓ ␈↓↓unify[x,y]␈↓␈αattempts␈αto␈αfind␈αthe␈αmost␈αgeneral␈αpattern␈αwhich␈αis␈αan␈αinstance␈αof␈αboth␈α␈↓↓x␈↓␈αand␈α␈↓↓y.␈↓
␈↓ ↓H␈↓If␈α
no␈α
such␈α
pattern␈α
exists␈α∞the␈α
it␈α
returns␈α
␈↓¬NO,␈α
␈↓otherwise␈α
it␈α∞returns␈α
a␈α
list␈α
of␈α
substitutions␈α∞which␈α
will
␈↓ ↓H␈↓convert both ␈↓↓x␈↓ and ␈↓↓y␈↓ into that pattern. Prove
␈↓ ↓H␈↓ ii) ␈↓↓∀x y: (unify[x, y] ≠ ␈↓¬NO ␈↓↓⊃ sublis[x, unify[x, y]] = sublis[y, unify[x, y]])␈↓
␈↓ ↓H␈↓ iii) ␈↓↓∀x y: (unify[x, y] = ␈↓¬NO ␈↓↓⊃ ∀s: sublis[x, s] ≠ sublis[y, s])␈↓
␈↓ ↓H␈↓ iv) ␈↓↓∀x y s: (sublis[x, s] = sublis[y, s] ⊃ ∃s1: ∀z: sublis[z, s] = sublis[z, unify[x, y] @ s1])␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓ ␈↓↓inst[x, y, s] ← ␈↓αif␈↓↓ s = ␈↓¬NO ␈↓↓␈↓αthen␈↓↓ ␈↓¬NO ␈↓↓␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓y ␈↓αthen␈↓↓ [␈↓αif␈↓↓ isvar y ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓ ␈↓↓{assoc[y, s]}[λz: ␈↓αif␈↓↓ ␈↓αn|␈↓↓z ␈↓αthen␈↓↓ [y . x] . s ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αd|␈↓↓z = x ␈↓αthen␈↓↓ s ␈↓αelse␈↓↓ ␈↓¬NO ␈↓↓]␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ y = x ␈↓αthen␈↓↓ s ␈↓αelse␈↓↓ ␈↓¬NO ␈↓↓]␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ ␈↓¬NO ␈↓↓␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ inst[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y, inst[␈↓αa|␈↓↓x, ␈↓αa|␈↓↓y, s]]␈↓
␈↓ ↓H␈↓ ␈↓↓isvar x ← x = ␈↓¬U ␈↓↓∨ x = ␈↓¬V ␈↓↓∨ x = ␈↓¬W ␈↓↓∨ x = ␈↓¬X ␈↓↓∨ x = ␈↓¬Y ␈↓↓∨ x = ␈↓¬Z ␈↓↓␈↓
␈↓ ↓H␈↓ ␈↓↓unify[x, y] ← ␈↓αif␈↓↓ x = y ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ isvar x ␈↓αthen␈↓↓ [␈↓αif␈↓↓ occur[x, y] ␈↓αthen␈↓↓ ␈↓¬NO ␈↓↓␈↓αelse␈↓↓ <x . y>]␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ isvar y ␈↓αthen␈↓↓ [␈↓αif␈↓↓ occur[y, x] ␈↓αthen␈↓↓ ␈↓¬NO ␈↓↓␈↓αelse␈↓↓ <y . x>]␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ [␈↓αat|␈↓↓x ∨ ␈↓αat|␈↓↓y] ␈↓αthen␈↓↓ ␈↓¬NO ␈↓↓␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ {unify[␈↓αa|␈↓↓x,␈↓αa|␈↓↓y]}␈↓
␈↓ ↓H␈↓ ␈↓↓[λs1: ␈↓αif␈↓↓ s1 = ␈↓¬NO ␈↓↓␈↓αthen␈↓↓ ␈↓¬NO ␈↓↓␈↓
␈↓ ↓H␈↓ ␈↓↓␈↓αelse␈↓↓ {unify[sublis[␈↓αd|␈↓↓x, s1],sublis[␈↓αd|␈↓↓y, s1]]}␈↓
␈↓ ↓H␈↓ ␈↓↓[λs2: ␈↓αif␈↓↓ s2 = ␈↓¬NO ␈↓↓␈↓αthen␈↓↓ ␈↓¬NO ␈↓↓␈↓αelse␈↓↓ s1 @ s2]]␈↓
␈↓ ↓H␈↓80␈↓ εH␈↓ H
␈↓ ↓H␈↓α␈↓ ¬{Chapter IV
␈↓ ↓H␈↓α␈↓ βVIMPURE PROGRAMS AND UNCLEAN PROGRAMS
␈↓ ↓H␈↓ Pure␈α
clean␈α∞LISP␈α
programs␈α∞admit␈α
the␈α
elegant␈α∞mathematical␈α
characterization␈α∞described␈α
and
␈↓ ↓H␈↓applied␈α∞in␈α∞Chapter␈α∂III.␈α∞ Specifically,␈α∞each␈α∂recursively␈α∞defined␈α∞pure␈α∂clean␈α∞LISP␈α∞function␈α∂can␈α∞be
␈↓ ↓H␈↓translated␈αinto␈αa␈αfunctional␈αequation␈αand␈αminimization␈αschema␈αin␈αa␈αfirst␈αorder␈αlanguage,␈αand␈αthe
␈↓ ↓H␈↓function and schema can be used to prove that the program meets its extensional specifications.
␈↓ ↓H␈↓ In␈α⊂this␈α∂chapter␈α⊂we␈α∂shall␈α⊂describe␈α∂some␈α⊂additional␈α∂features␈α⊂of␈α∂practical␈α⊂LISP␈α⊂systems␈α∂for
␈↓ ↓H␈↓which␈α
good␈α
simple␈α
mathematical␈α
characterizations␈α
have␈α
not␈α
yet␈α
been␈α
found.␈α
Every␈α
computation
␈↓ ↓H␈↓that␈αcan␈αbe␈αdone␈α
with␈αthese␈αfeatures␈αcan␈α
be␈αdone␈αin␈αpure␈α
clean␈αLISP,␈αbut␈αnevertheless␈α
there␈αare
␈↓ ↓H␈↓often␈α
good␈α
reasons␈α
for␈α
using␈α
these␈α
features.␈α
We␈α
shall␈α
examine␈α
the␈α
features␈α
themselves␈α∞and␈α
also
␈↓ ↓H␈↓the criteria that determine when they should be used in preference to pure clean LISP.
␈↓ ↓H␈↓ All␈α⊗these␈α⊗features␈α⊗can␈α⊗be␈α∃formalized␈α⊗in␈α⊗terms␈α⊗of␈α⊗their␈α∃effect␈α⊗on␈α⊗the␈α⊗"state␈α⊗of␈α∃the
␈↓ ↓H␈↓computation",␈αand␈αthis␈αhas␈αbeen␈αdone␈αby␈αMichael␈αGordon␈α[1975],␈αbut␈αhis␈αformalizations␈αseem␈αtoo
␈↓ ↓H␈↓complicated␈αfor␈αelementary␈αexposition.␈α The␈αgeneral␈αidea␈αis␈αto␈αintroduce␈αa␈αstate␈αvariable␈α␈↓πx␈↓␈αand␈αto
␈↓ ↓H␈↓describe the "execution" of expressions as functions on states that produce new states.
␈↓ ↓H␈↓ The␈αextreme␈αpractical␈α"LISP␈αhacker"␈αtakes␈αthe␈αfollowing␈αpoint␈αof␈αview.␈α Executing␈αthe␈α
code
␈↓ ↓H␈↓corresponding␈α
to␈α
a␈α
LISP␈α
"function"␈α
can␈α
have␈α∞effects␈α
that␈α
go␈α
beyond␈α
providing␈α
the␈α
value␈α∞of␈α
the
␈↓ ↓H␈↓function␈α
applied␈α
to␈α
its␈α
arguments␈α
for␈α
further␈α
computation.␈α
These␈α
effects␈α
may␈α
include␈α
modification
␈↓ ↓H␈↓of␈α∞the␈α∞LISP␈α∞stack,␈α∞the␈α
free␈α∞storage␈α∞mechanism,␈α∞the␈α∞contents␈α
of␈α∞property␈α∞lists,␈α∞and␈α∞the␈α∞values␈α
of
␈↓ ↓H␈↓variables.␈α
By␈α∞including␈α
"functions"␈α
whose␈α∞programs␈α
have␈α
such␈α∞side-effects␈α
in␈α
a␈α∞larger␈α
program
␈↓ ↓H␈↓one␈α
can␈α
often␈αcompute␈α
answers␈α
more␈α
conveniently␈αthan␈α
in␈α
pure␈α
LISP.␈α From␈α
this␈α
point␈α
of␈αview,
␈↓ ↓H␈↓the fact that LISP programs look like pure function definitions is only a notational convenience.
␈↓ ↓H␈↓ Programs␈α⊃that␈α⊂take␈α⊃totally␈α⊂uninhibited␈α⊃advantage␈α⊂of␈α⊃side-effects␈α⊂are␈α⊃usually␈α⊃difficult␈α⊂to
␈↓ ↓H␈↓understand,␈αdebug␈α
and␈αmodify.␈α This␈α
has␈αled␈αto␈α
experienced␈αprogrammers␈αto␈α
use␈αthese␈αfeatures␈α
in
␈↓ ↓H␈↓moderation.␈α∞ That␈α∞such␈α
programs␈α∞are␈α∞not␈α
accessible␈α∞to␈α∞present␈α
proof␈α∞techniques␈α∞is␈α∞of␈α
secondary
␈↓ ↓H␈↓interest␈α
to␈αmost␈α
applied␈αprogrammers.␈α
In␈αthe␈α
future␈α
we␈αexpect␈α
that␈αlanguage␈α
designers␈αwill␈α
reduce
␈↓ ↓H␈↓the␈α⊗need␈α⊗for␈α⊗side-effect␈α⊗programming␈α⊗achieving␈α⊗their␈α⊗results␈α⊗by␈α⊗more␈α⊗direct␈α↔means,␈α⊗and
␈↓ ↓H␈↓programmers␈α∞will␈α∞come␈α∞to␈α∞see␈α∞debugging␈α∂a␈α∞correctness␈α∞proof␈α∞as␈α∞a␈α∞more␈α∞worthwile␈α∂activity␈α∞than
␈↓ ↓H␈↓debugging␈αby␈αexamples,␈αbecause␈αit␈αterminates␈αwith␈αa␈αconclusive␈αproof␈αthat␈αthe␈αprogram␈α
meets␈αits
␈↓ ↓H␈↓specifications.
␈↓ ↓H␈↓ In␈α
this␈α
chapter␈α
we␈α
describe␈α
some␈α
of␈α
these␈α
methods␈α
of␈α
programming␈α
together␈α
with␈αsome␈α
ideas
␈↓ ↓H␈↓on how to treat them theoretically.
␈↓ ↓H␈↓1. ␈↓αSequential (ALGOL-like) programs.␈↓
␈↓ ↓H␈↓ Sequential␈α∪programs␈α∪are␈α∀impure␈α∪(by␈α∪definition),␈α∪but␈α∀can␈α∪be␈α∪clean␈α∪-␈α∀provided␈α∪certain
␈↓ ↓H␈↓restrictions␈αare␈αobserved.␈α The␈αexternal␈αnotation␈αfor␈αsequential␈αprograms␈αis␈αadapted␈αfrom␈αthat␈αof
␈↓ ↓H␈↓ALGOL 60. We allow as a term an expression of the form
␈↓ ↓H␈↓␈↓ βx␈↓αprogram␈↓[[<variable list>] <statement list>],
␈↓ ↓H␈↓␈↓ ¬wChapter IV␈↓ *81
␈↓ ↓H␈↓where␈α
<variable␈αlist>␈α
is␈αa␈α
list␈αof␈α
variables␈αlocal␈α
to␈αthe␈α
program,␈αand␈α
<statement␈αlist>␈α
is␈αa␈α
list␈αof␈α
the
␈↓ ↓H␈↓statements␈α∞of␈α∞the␈α
program.␈α∞ As␈α∞in␈α∞ALGOL␈α
60,␈α∞the␈α∞statements␈α
are␈α∞separated␈α∞by␈α∞semicolons,␈α
and
␈↓ ↓H␈↓any statement may be preceded by a label followed by a colon.
␈↓ ↓H␈↓ The statements are of the following kinds:
␈↓ ↓H␈↓ 1. Assignment statements of the form
␈↓ ↓H␈↓␈↓ βx<left hand side> ← <right hand side>,
␈↓ ↓H␈↓where␈α⊃<left␈α⊂hand␈α⊃side>␈α⊃is␈α⊂a␈α⊃variable,␈α⊂possibly␈α⊃subscripted,␈α⊃and␈α⊂<right␈α⊃hand␈α⊂side>␈α⊃is␈α⊃a␈α⊂LISP
␈↓ ↓H␈↓expression that can be evaluated.
␈↓ ↓H␈↓ 2. ␈↓αgo to␈↓ statements of the form
␈↓ ↓H␈↓␈↓ βx␈↓αgo to␈↓ <label>
␈↓ ↓H␈↓where␈α⊂<label>␈α⊂is␈α⊂ an␈α⊂expression␈α⊂that␈α⊂evaluates␈α∂to␈α⊂a␈α⊂label.␈α⊂ Since␈α⊂labels␈α⊂are␈α⊂atoms␈α⊂in␈α∂internal
␈↓ ↓H␈↓notation,␈α
any␈α
expression␈α
evaluating␈α
to␈α
an␈α
atom␈α
may␈αbe␈α
used,␈α
but␈α
the␈α
usual␈α
case␈α
is␈α
a␈αconditional
␈↓ ↓H␈↓expression␈αwherein␈α
the␈αsecond␈α
element␈αof␈α
each␈αpair␈αis␈α
an␈αatom.␈α
Should␈αthe␈α
resulting␈αexpression
␈↓ ↓H␈↓not be an atom or should that atom not be used as a label, an error message will be generated.
␈↓ ↓H␈↓ 3. Conditional statements which have the form
␈↓ ↓H␈↓␈↓ βx␈↓αif␈↓ <p1> ␈↓αthen␈↓ <s1> ␈↓αelse␈↓ ␈↓αif␈↓ ... ␈↓αelse␈↓ ␈↓αif␈↓ <pn> ␈↓αthen␈↓ <sn>,
␈↓ ↓H␈↓where␈α∞the␈α
<pi>␈α∞are␈α
propositional␈α∞terms␈α
having␈α∞truth␈α
values,␈α∞and␈α
<si>␈α∞is␈α
any␈α∞statement.␈α
Notice
␈↓ ↓H␈↓that␈αconditional␈α
statements␈αterminate␈αwith␈α
a␈α␈↓αthen␈↓␈α
clause␈αas␈αdo␈α
conditional␈αexpressions␈α
in␈αgeneral
␈↓ ↓H␈↓in␈α
internal␈α
form␈α
If␈α
none␈αof␈α
the␈α
propositional␈α
terms␈α
are␈α
true␈αthe␈α
statement␈α
has␈α
no␈α
effect.␈α (Unless␈α
of
␈↓ ↓H␈↓course there were side effects in the evaluation of the propositional terms.)
␈↓ ↓H␈↓ 4. ␈↓αreturn␈↓ statements of the form
␈↓ ↓H␈↓␈↓ βx␈↓αreturn␈↓ <expression>
␈↓ ↓H␈↓where␈α<expression>␈αis␈αan␈αarbitrary␈αexpression.␈α The␈αeffect␈αof␈αexecuting␈αthis␈αstatement␈αis␈αto␈αreturn
␈↓ ↓H␈↓from the program giving the program as a term the value of <expression>.
␈↓ ↓H␈↓ As an example, we might write ␈↓↓reverse␈↓ as follows:
␈↓ ↓H␈↓↓␈↓ βxreverse u ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[v]
␈↓ ↓H␈↓↓␈↓ ∧xv ← ␈↓¬NIL␈↓↓;
␈↓ ↓H␈↓↓1.1)␈↓ ∧8a:␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ v;
␈↓ ↓H␈↓↓␈↓ ∧xv ← ␈↓αa|␈↓↓u . v;
␈↓ ↓H␈↓↓␈↓ ∧xu ← ␈↓αd|␈↓↓v;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ a].
␈↓ ↓H␈↓ The internal form of the same program is
␈↓ ↓H␈↓82␈↓ ¬wChapter IV␈↓ H
␈↓ ↓H␈↓¬␈↓ βx(DEFUN REVERSE (U)
␈↓ ↓H␈↓¬␈↓ ∧8(PROG (V)
␈↓ ↓H␈↓¬␈↓ ∧x(SETQ V NIL)
␈↓ ↓H␈↓¬␈↓ ∧xA
␈↓ ↓H␈↓¬␈↓ ∧x(COND ((NULL U) (RETURN V)))
␈↓ ↓H␈↓¬␈↓ ∧x(SETQ V (CONS (CAR U) V))
␈↓ ↓H␈↓¬␈↓ ∧x(SETQ U (CDR U))
␈↓ ↓H␈↓¬␈↓ ∧x(GO A)))
␈↓ ↓H␈↓where␈αthe␈αparagraphing␈αis␈αonly␈αfor␈αthe␈αreader's␈αconvenience.␈α Notice␈αthat␈αin␈αinternal␈αform,␈αlabels
␈↓ ↓H␈↓are statements rather than attachments to statements.
␈↓ ↓H␈↓ In general the internal form of a program term is the following:
␈↓ ↓H␈↓␈↓ ∧d(␈↓¬PROG ␈↓ <variable list> <s1> ... <sn>)
␈↓ ↓H␈↓and the four types of statements have the forms:
␈↓ ↓H␈↓␈↓ βx(␈↓¬SETQ ␈↓<variable> <expression>)
␈↓ ↓H␈↓␈↓ βx(␈↓¬GO ␈↓<label>)
␈↓ ↓H␈↓␈↓ βx(␈↓¬COND ␈↓(<p1> <s1>) ... (<pn> <sn>))
␈↓ ↓H␈↓␈↓ βx(␈↓¬RETURN ␈↓<expression>)
␈↓ ↓H␈↓In␈α⊃most␈α∩LISP␈α⊃implementations␈α∩arrays␈α⊃are␈α⊃treated␈α∩specially␈α⊃and␈α∩subscripted␈α⊃variables␈α∩are␈α⊃not
␈↓ ↓H␈↓allowed␈α∞as␈α
first␈α∞arguments␈α
to␈α∞␈↓¬SETQ.␈α
␈↓␈α∞The␈α
␈↓¬STORE␈α∞␈↓command␈α
must␈α∞be␈α
used␈α∞in␈α
order␈α∞to␈α∞change␈α
an
␈↓ ↓H␈↓array element. We will say more about arrays in the remarks at the end of this section.
␈↓ ↓H␈↓ As␈α
further␈α
examples␈α
of␈α
how␈α
␈↓αprogram␈↓␈α
might␈α∞be␈α
used,␈α
here␈α
are␈α
some␈α
ways␈α
we␈α∞might␈α
write
␈↓ ↓H␈↓␈↓↓append:␈↓
␈↓ ↓H␈↓1.2)␈↓ βA␈↓↓u * v ← ␈↓αprogram␈↓↓[[] ␈↓αreturn␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]]␈↓,
␈↓ ↓H␈↓is just a trivial rewrite of the recursive definition.
␈↓ ↓H␈↓↓␈↓ βxu * v ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[w]
␈↓ ↓H␈↓↓1.3)␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ v;
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v];
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αreturn␈↓↓ w]
␈↓ ↓H␈↓is␈αalmost␈α
as␈αclose␈α
to␈αthe␈α
pure␈αLISP␈α
form.␈α If␈αwe␈α
want␈αto␈α
replace␈αthe␈α
recursion␈αby␈α
a␈αloop,␈α
we␈αcan
␈↓ ↓H␈↓write
␈↓ ↓H␈↓␈↓ ¬wChapter IV␈↓ *83
␈↓ ↓H␈↓↓␈↓ βxu * v ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[u1,v1,w]
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓¬NIL␈↓↓;
␈↓ ↓H␈↓↓␈↓ ∧xu1 ← u;
␈↓ ↓H␈↓↓␈↓ ∧xv1 ← v;
␈↓ ↓H␈↓↓␈↓ ∧8a:␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓u1 ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ b;
␈↓ ↓H␈↓↓1.4)␈↓ ∧xw ← ␈↓αa|␈↓↓u1 . w;
␈↓ ↓H␈↓↓␈↓ ∧xu1 ← ␈↓αd|␈↓↓u;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ a;
␈↓ ↓H␈↓↓␈↓ ∧8b:␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ v1;
␈↓ ↓H␈↓↓␈↓ ∧xv1 ← ␈↓αa|␈↓↓w . v1;
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓αd|␈↓↓w;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ b].
␈↓ ↓H␈↓This corresponds to the recursive program
␈↓ ↓H␈↓↓␈↓ βxu * v ← app[u,v,␈↓¬NIL␈↓↓]
␈↓ ↓H␈↓↓1.5)
␈↓ ↓H␈↓↓␈↓ βxapp[u,v,w] ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ [␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ app[u,␈↓αa|␈↓↓w . v,␈↓αd|␈↓↓w]]
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ app[␈↓αd|␈↓↓u,v,␈↓αa|␈↓↓u . w]
␈↓ ↓H␈↓which can save some storage if it is compiled or interpreted without using the stack.
␈↓ ↓H␈↓ We␈αshall␈αnot␈αtreat␈αthe␈αmathematics␈αof␈αsequential␈αprograms␈αfully␈αat␈αthis␈αpoint.␈α However,␈αwe
␈↓ ↓H␈↓note␈α
that␈α
under␈α
certain␈α
conditions␈α
a␈α
sequential␈α
program␈α
that␈α
occurs␈α
as␈α
a␈α
term␈α
can␈α
be␈α
replaced␈αby␈α
a
␈↓ ↓H␈↓term␈α
involving␈α
only␈α
functional␈α
programs.␈α
This␈α
means␈α
that␈α
the␈α
theory␈α
developed␈α
in␈α
Chapter␈α
III
␈↓ ↓H␈↓can␈α∞be␈α∞applied␈α∞to␈α∞such␈α∞programs.␈α∞ The␈α
main␈α∞property␈α∞that␈α∞a␈α∞sequential␈α∞program␈α∞must␈α∞have␈α
in
␈↓ ↓H␈↓order␈αto␈αbe␈αreplaced␈α
when␈αit␈αoccurs␈αas␈αa␈α
term␈αby␈αa␈αfunctional␈α
program␈αcomputing␈αits␈αvalue␈αis␈α
that
␈↓ ↓H␈↓the␈α⊂value␈α⊂of␈α⊃any␈α⊂expression␈α⊂containing␈α⊃the␈α⊂program␈α⊂as␈α⊂a␈α⊃term␈α⊂remains␈α⊂unchanged␈α⊃when␈α⊂the
␈↓ ↓H␈↓program␈α
is␈α
replaced␈α
by␈α
its␈α
value.␈α
Or␈α
more␈α
briefly,␈α
the␈α
term␈α
must␈α
be␈α
without␈α
(external)␈α
side␈α
effects.
␈↓ ↓H␈↓It␈α
is␈α
sufficient␈α
to␈α
know␈α
that␈α
all␈α
assignments␈α∞are␈α
to␈α
variables␈α
local␈α
to␈α
the␈α
program␈α
and␈α∞that␈α
each
␈↓ ↓H␈↓label␈αrefered␈αto␈αoccurs␈αin␈αthe␈αprogram.␈α For␈αthe␈αprogram␈αto␈αmake␈αsense␈αlocal␈αvariables␈αneed␈αto␈αbe
␈↓ ↓H␈↓initialed␈α∞and␈α∂labels␈α∞should␈α∂be␈α∞unique␈α∂although␈α∞these␈α∂matters␈α∞could␈α∂be␈α∞handled␈α∂by␈α∞establishing
␈↓ ↓H␈↓some␈αdefault␈αconventions.␈α We␈α
will␈αdiscuss␈αtransforming␈αsequential␈α
programs␈αand␈αrelated␈αtopics␈α
in
␈↓ ↓H␈↓greater detail in Chapter V.
␈↓ ↓H␈↓ There␈α⊂are␈α⊂three␈α⊂circumstances␈α⊂in␈α∂which␈α⊂sequential␈α⊂programs␈α⊂are␈α⊂preferred␈α⊂to␈α∂functional
␈↓ ↓H␈↓programs:
␈↓ ↓H␈↓ 1.␈αIn␈αsome␈αcases␈αand␈αfor␈αsome␈αpeople,␈αit␈αis␈αeasier␈αto␈αthink␈αabout␈αhow␈αto␈αgo␈αfrom␈αthe␈αinitial
␈↓ ↓H␈↓conditions␈α
step-by-step␈α
to␈α
the␈α
final␈α
result␈α
than␈α
to␈α
think␈α
about␈α
how␈α
the␈α
final␈α
result␈α
can␈αbe␈α
obtained
␈↓ ↓H␈↓from␈α∩easier␈α∩cases.␈α∩ There␈α∩seems␈α∩to␈α∩be␈α∩a␈α∩matter␈α∩of␈α∩taste␈α∩to␈α∩a␈α∩substantial␈α∩extent,␈α∪though␈α∩the
␈↓ ↓H␈↓functional form seems to have clear conceptual advantages for functions like ␈↓↓append␈↓ and ␈↓↓subst␈↓.
␈↓ ↓H␈↓ 2.␈αWhen␈αthere␈αare␈αa␈αlarge␈αnumber␈αof␈αvariables,␈αit␈αcan␈αturn␈αout␈αthat␈αthe␈αnecessary␈αfunctions
␈↓ ↓H␈↓have an unwieldy number of arguments.
␈↓ ↓H␈↓84␈↓ ¬wChapter IV␈↓ H
␈↓ ↓H␈↓ 3.␈α⊃When␈α∩we␈α⊃are␈α∩considering␈α⊃a␈α∩program␈α⊃that␈α∩interacts␈α⊃with␈α∩an␈α⊃environment␈α∩instead␈α⊃of
␈↓ ↓H␈↓producing an answer, the sequential form may be required, although this hasn't been proved.
␈↓ ↓H␈↓Remarks:
␈↓ ↓H␈↓ 1.␈α
Although␈α
␈↓αreturn␈↓␈α
and␈α
␈↓αgo to␈↓␈α
statements␈α
in␈α
LISP␈α
have␈α
no␈α
meaning␈α
unless␈α
they␈α
occur␈α
inside
␈↓ ↓H␈↓a␈α␈↓αprogram␈↓,␈αan␈αassignment␈α
statement␈αmay␈αbe␈αregarded␈αas␈α
an␈αprogram␈αterm.␈α Thus␈α
␈↓↓[x ← ␈↓αa|␈↓↓y] . z␈↓␈αis
␈↓ ↓H␈↓an abbreviation for ␈↓↓␈↓αprogram␈↓↓[[] x ← ␈↓αa|␈↓↓y; ␈↓αreturn␈↓↓ x] . z␈↓.
␈↓ ↓H␈↓ 2.␈α∞Besides␈α∞␈↓¬SETQ,␈α∞␈↓most␈α∞LISPs␈α∞allow␈α∞assignments␈α∞of␈α∞the␈α∞form␈α∞ (␈↓¬SET␈α∞␈↓<exp1>␈α∞<exp2>),␈α∞where
␈↓ ↓H␈↓<exp1>␈α⊂is␈α⊂evaluated␈α⊂to␈α⊂determine␈α⊂to␈α⊂what␈α⊃variable␈α⊂the␈α⊂assignment␈α⊂is␈α⊂to␈α⊂be␈α⊂made.␈α⊃ If␈α⊂<exp1>
␈↓ ↓H␈↓doesn't evaluate to a variable, an error is signalled.
␈↓ ↓H␈↓ 3.␈α
In␈α
the␈α
conditional␈α
statement␈α
it␈α
is␈α
convenient␈α
to␈α
allow␈α
a␈α
list␈α
of␈α
statements␈α
as␈α
the␈α
␈↓αthen␈↓␈α
clause
␈↓ ↓H␈↓rather␈α
than␈αa␈α
single␈αstatement.␈α
This␈α
often␈αallows␈α
a␈αmore␈α
natural␈α
organization␈αof␈α
the␈αprogram␈α
and
␈↓ ↓H␈↓causes no difficulty in the rewriting of clean sequential programs as functional programs.
␈↓ ↓H␈↓ 4.␈α∪In␈α∪writing␈α∪␈↓αprogram␈↓s␈α∪in␈α∪external␈α∪form␈α∩we␈α∪will␈α∪often␈α∪use␈α∪the␈α∪convention␈α∪that␈α∩each
␈↓ ↓H␈↓statement␈αis␈αwritten␈αon␈αa␈αseparate␈αline.␈α This␈αmeans␈αthat␈αthe␈α";"␈αseparator␈αin␈αnot␈αneeded␈αand␈αmay
␈↓ ↓H␈↓be omitted in this case.
␈↓ ↓H␈↓ 5.␈α
As␈αmentioned␈α
earlier,␈αin␈α
addition␈αto␈α
simple␈α
variables␈αwhich␈α
may␈αbe␈α
assigned␈αvalues␈α
using
␈↓ ↓H␈↓the␈α
assignment␈α∞statement,␈α
LISP␈α∞admits␈α
arrays.␈α∞ An␈α
array␈α
may␈α∞in␈α
principle␈α∞have␈α
any␈α∞number␈α
of
␈↓ ↓H␈↓dimensions.␈α⊂ If␈α⊂␈↓↓foo␈↓␈α∂is␈α⊂a␈α⊂2-dimensional␈α⊂array␈α∂then␈α⊂␈↓↓foo[i,j]␈↓␈α⊂can␈α⊂be␈α∂thought␈α⊂of␈α⊂as␈α⊂a␈α∂complicated
␈↓ ↓H␈↓variable␈α∞name.␈α∞ It␈α∞evaluates␈α∞to␈α∞the␈α∞jth␈α∞element␈α∞of␈α∞the␈α∞ith␈α∞row␈α∞of␈α∞␈↓↓foo␈↓␈α∞and␈α∞it␈α∞can␈α∞be␈α∞assigned␈α∞a
␈↓ ↓H␈↓value␈α⊂using␈α∂the␈α⊂function␈α⊂␈↓↓store.␈↓␈α∂ Thus␈α⊂␈↓↓store[foo[i,j],e]␈↓␈α⊂assigns␈α∂to␈α⊂␈↓↓foo[i,j]␈↓␈α⊂the␈α∂value␈α⊂of␈α⊂␈↓↓e.␈↓␈α∂ The
␈↓ ↓H␈↓internal␈α∪forms␈α∪for␈α∪these␈α∩terms␈α∪are␈α∪␈↓¬(FOO I J)␈α∪␈↓␈α∩and␈α∪ ␈↓¬(STORE (FOO I J) E).␈α∪␈↓␈α∪For␈α∪additional␈α∩array
␈↓ ↓H␈↓functions␈α
and␈α
features␈α
the␈αreader␈α
should␈α
consult␈α
the␈α
programmers␈αmanual␈α
for␈α
the␈α
version␈αof␈α
LISP
␈↓ ↓H␈↓being used, as these vary.
␈↓ ↓H␈↓ Recall␈α⊃that␈α⊂in␈α⊃external␈α⊂form␈α⊃we␈α⊂allowed␈α⊃subscripted␈α⊂variables␈α⊃in␈α⊃assignment␈α⊂statements.
␈↓ ↓H␈↓Thus␈α∂␈↓↓store[foo[i,u],e]␈↓␈α∂can␈α∂also␈α∂be␈α∂written␈α∂␈↓↓foo[i,j] ← e␈↓.␈α∂ It␈α∂would␈α∂not␈α∂be␈α∂difficult␈α∂to␈α∂modify␈α∂the
␈↓ ↓H␈↓␈↓¬SETQ␈α∂␈↓of␈α∂LISP␈α∂to␈α∞accept␈α∂subscripted␈α∂variables,␈α∂although␈α∂there␈α∞may␈α∂be␈α∂practical␈α∂reasons␈α∂for␈α∞not
␈↓ ↓H␈↓doing␈α⊂so.␈α⊂ Note␈α⊂that␈α⊂accessing␈α⊂an␈α⊂array␈α⊂element␈α⊂has␈α⊂the␈α⊂same␈α⊂form␈α⊂as␈α⊂a␈α⊂function␈α⊃call.␈α⊂ LISP
␈↓ ↓H␈↓distinguishes␈α⊃the␈α⊃two␈α⊃by␈α⊃determing␈α⊃from␈α⊃the␈α⊃property␈α⊃list␈α⊃of␈α⊃␈↓¬FOO␈α⊃whether␈α⊃␈↓it␈α⊃is␈α⊃an␈α⊃array,␈α⊃or
␈↓ ↓H␈↓function.␈α
This␈αwill␈α
be␈αdiscussed␈α
further␈αin␈α
the␈αsection␈α
on␈αproperty␈α
lists.␈α Arrays␈α
are␈α
are␈αpratical
␈↓ ↓H␈↓for␈α∞two␈α
reasons.␈α∞ One␈α
is␈α∞quick␈α
access␈α∞to␈α
individual␈α∞elements,␈α
the␈α∞other␈α
is␈α∞that␈α
they␈α∞are␈α
updated
␈↓ ↓H␈↓rather than copied which saves space.
␈↓ ↓H␈↓ 6.␈α⊗For␈α∃additional␈α⊗discussion␈α⊗of␈α∃the␈α⊗method␈α⊗for␈α∃converting␈α⊗sequential␈α⊗programs␈α∃into
␈↓ ↓H␈↓functional programs see McCarthy [1962b].
␈↓ ↓H␈↓α␈↓ ε
Exercises
␈↓ ↓H␈↓ 1.␈αWrite␈α
a␈αLISP␈α
predicate␈αthat␈α
will␈αdetermine␈α
whether␈αa␈α
␈↓¬PROG␈α␈↓␈α
in␈αinternal␈α
form␈αsatisfies␈α
the
␈↓ ↓H␈↓above conditions for replacement by a functional term.
␈↓ ↓H␈↓ 2.␈α
Given␈α
that␈α∞a␈α
␈↓¬PROG␈α
␈↓satisfies␈α
the␈α∞conditions,␈α
write␈α
a␈α
LISP␈α∞functions␈α
to␈α
convert␈α∞the␈α
␈↓¬PROG
␈↓ ↓H␈↓¬␈↓expression and give the a list of the required auxiliary function definitions.
␈↓ ↓H␈↓␈↓ ¬wChapter IV␈↓ *85
␈↓ ↓H␈↓2. ␈↓αPseudo-functions that modify list structures␈↓
␈↓ ↓H␈↓ In␈α∂pure␈α∂LISP,␈α⊂list␈α∂structure␈α∂is␈α⊂never␈α∂changed.␈α∂ ␈↓↓car␈↓␈α⊂and␈α∂␈↓↓cdr␈↓␈α∂merely␈α⊂move␈α∂about␈α∂in␈α⊂a␈α∂list
␈↓ ↓H␈↓structure,␈αand␈α␈↓↓cons␈↓␈αcreates␈αnew␈αlist␈αstructure␈α
from␈αthe␈αfree␈αstorage␈αlist.␈α Of␈αcourse,␈αa␈α
variable␈αcan
␈↓ ↓H␈↓get␈αa␈αnew␈α
value␈αthat␈αcorresponds,␈αfor␈α
instance,␈αto␈αputting␈αan␈α
element␈αon␈αthe␈αend␈α
of␈αa␈αlist,␈α
but␈αin
␈↓ ↓H␈↓pure␈α∞LISP␈α
this␈α∞can␈α
only␈α∞be␈α
accomplished␈α∞by␈α∞creating␈α
new␈α∞list␈α
structure.␈α∞ In␈α
this␈α∞section␈α∞we␈α
will
␈↓ ↓H␈↓discuss␈αoperations␈αthat␈αactually␈αmodify␈αlist␈αstructure.␈α They␈αoften␈αhave␈αefficiency␈α
advantages,␈αbut
␈↓ ↓H␈↓their␈α∩use␈α∩interferes␈α∩with␈α∩saving␈α∩memory␈α⊃by␈α∩using␈α∩merging␈α∩list␈α∩structure,␈α∩and␈α∩techniques␈α⊃for
␈↓ ↓H␈↓proving correctness of such programs are not well developed.
␈↓ ↓H␈↓ The␈α∀simplest␈α∀way␈α∀to␈α∀express␈α∀operations␈α∀that␈α∀modify␈α∀list␈α∀structure␈α∀is␈α∀in␈α∀the␈α∀form␈α∪of
␈↓ ↓H␈↓assignments␈α∀to␈α∀␈↓↓car-cdr␈↓␈α∀chains␈α∀of␈α∀variables,␈α∀e.g.␈α∀we␈α∀may␈α∀write␈α∀␈↓↓␈↓αada|␈↓↓x ← y . z␈↓.␈α∀ The␈α∀effect␈α∀of
␈↓ ↓H␈↓executing␈α
this␈α
statement␈α
is␈α
to␈α
replace␈α
the␈α
␈↓αada|␈↓␈α
part␈α
of␈α
the␈α
list␈α
structure␈α
pointed␈α
to␈α
by␈α
␈↓↓x␈↓␈α∞with␈α
the
␈↓ ↓H␈↓value␈α⊂of␈α∂the␈α⊂right␈α∂hand␈α⊂side.␈α∂ As␈α⊂a␈α∂term,␈α⊂the␈α∂statement␈α⊂takes␈α∂the␈α⊂value␈α∂assigned.␈α⊂ In␈α∂internal
␈↓ ↓H␈↓notation,␈α∂we␈α∂use␈α∞the␈α∂pseudo-functions␈α∂␈↓¬(RPLACA X Y)␈↓␈α∂and␈α∞␈↓¬(RPLACD X Y)␈↓␈α∂which␈α∂correspond␈α∂to␈α∞the
␈↓ ↓H␈↓statements ␈↓↓␈↓αa|␈↓↓x ← y␈↓ and ␈↓↓␈↓αd|␈↓↓x ← y␈↓ respectively.
␈↓ ↓H␈↓ ␈↓¬RPLACA␈α␈↓and␈α␈↓¬RPLACD␈α␈↓are␈αhighly␈αunclean.␈α The␈αeffect␈αof␈αevaluating␈αan␈αexpression␈αinvolving
␈↓ ↓H␈↓␈↓¬RPLACA␈α
␈↓or␈α
␈↓¬RPLACD␈α
␈↓␈α
depends␈αon␈α
the␈α
state␈α
of␈α
list␈α
structure␈αand␈α
not␈α
merely␈α
on␈α
the␈αS-expressions␈α
that
␈↓ ↓H␈↓the␈α∞variables␈α∞have␈α∞as␈α∞values.␈α∞ Suppose,␈α∞for␈α∞example,␈α∞that␈α∞the␈α∞variables␈α∞␈↓¬X␈α∞␈↓and␈α∞␈↓¬Y␈α∞␈↓each␈α∞have␈α
the
␈↓ ↓H␈↓value␈α
␈↓¬(A B),␈α
␈↓and␈αconsider␈α
the␈α
effect␈α
of␈α␈↓¬(SETQ Z (RPLACA (CDR X) C))␈↓.␈α
␈↓¬Z␈α
␈↓gets␈α
the␈αvalue␈α
␈↓¬(C),␈α
␈↓␈α
and␈α␈↓¬X
␈↓ ↓H␈↓¬␈↓gets␈αthe␈αvalue␈α␈↓¬(A C),␈α␈↓but␈αthe␈αnew␈αvalue␈αof␈α␈↓¬Y␈α␈↓depends␈αon␈αwhether␈αits␈αcopy␈αof␈α␈↓¬(A B)␈α␈↓is␈αthe␈αsame␈αlist
␈↓ ↓H␈↓structure as ␈↓¬X␈↓'s copy. If yes, the new value of ␈↓¬Y ␈↓is also ␈↓¬(A C); ␈↓otherwise its value remains ␈↓¬(A B). ␈↓
␈↓ ↓H␈↓ The␈α
uncleanliness␈α
of␈α
␈↓¬RPLACA␈α
␈↓and␈α∞␈↓¬RPLACD␈α
␈↓means␈α
that␈α
the␈α
programmer␈α
must␈α∞know␈α
exactly
␈↓ ↓H␈↓what list structures merge. A typical application is the pseudo-function ␈↓↓nconc␈↓ defined by
␈↓ ↓H␈↓↓␈↓ βxnconc[u,v] ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[w]
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αn|␈↓↓u ← ␈↓αreturn␈↓↓ v;
␈↓ ↓H␈↓↓␈↓ ∧xw ← u;
␈↓ ↓H␈↓↓2.1)␈↓ ∧8loop:
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓w ␈↓αthen␈↓↓ [␈↓αd|␈↓↓w ← v; ␈↓αreturn␈↓↓ u];
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓αd|␈↓↓w;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ loop].
␈↓ ↓H␈↓␈↓↓nconc␈↓ can also be given a definition that has the form of a recursive program, namely
␈↓ ↓H␈↓↓␈↓ βxnconc[u, v] ← nconc1[u, u, v]
␈↓ ↓H␈↓↓2.2)
␈↓ ↓H␈↓↓␈↓ βxnconc1[u, w, v] ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ v
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓w ␈↓αthen␈↓↓ [λz: u][rplacd[w, v]]
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ nconc1[u, ␈↓αd|␈↓↓w, v].
␈↓ ↓H␈↓ The␈α∂value␈α∂of␈α∞␈↓↓nconc[u, v]␈↓␈α∂is␈α∂just␈α∞␈↓↓u * v,␈↓␈α∂but␈α∂it␈α∞achieves␈α∂this␈α∂result␈α∞by␈α∂modifying␈α∂the␈α∞last
␈↓ ↓H␈↓86␈↓ ¬wChapter IV␈↓ H
␈↓ ↓H␈↓element␈α
of␈α
␈↓↓u␈↓␈α
to␈α
point␈α
to␈α␈↓↓v.␈↓␈α
It␈α
is␈α
typically␈α
used␈α
when␈αno␈α
other␈α
variable␈α
points␈α
to␈α
a␈α
list␈αstructure
␈↓ ↓H␈↓that␈α⊃merges␈α⊃into␈α∩␈↓↓u,␈↓␈α⊃and␈α⊃the␈α∩old␈α⊃value␈α⊃of␈α∩␈↓↓u␈↓␈α⊃will␈α⊃not␈α∩be␈α⊃used␈α⊃again.␈α∩ In␈α⊃that␈α⊃case,␈α∩␈↓↓nconc␈↓␈α⊃is
␈↓ ↓H␈↓advantageous,␈α∂because␈α∂it␈α⊂does␈α∂no␈α∂␈↓↓cons␈↓es,␈α⊂and␈α∂thus␈α∂can't␈α⊂initiate␈α∂garbage␈α∂collection.␈α⊂ No␈α∂formal
␈↓ ↓H␈↓methods exist at present for proving that these conditions are met.
␈↓ ↓H␈↓ ␈↓¬RPLACD␈α⊃␈↓can␈α⊃also␈α⊃be␈α⊃used␈α⊃to␈α⊃insert␈α∩an␈α⊃element␈α⊃into␈α⊃the␈α⊃middle␈α⊃of␈α⊃a␈α⊃list.␈α∩ Suppose,␈α⊃for
␈↓ ↓H␈↓example,␈αthat␈αwe␈αwish␈αto␈αinsert␈αthe␈αatom␈α␈↓¬B␈α␈↓after␈αevery␈αoccurrence␈αof␈αthe␈αatom␈α␈↓¬A␈α␈↓in␈αa␈αlist␈α␈↓↓u.␈↓␈α We
␈↓ ↓H␈↓can␈αdefine␈αa␈αpure␈αLISP␈αfunction␈αthat␈αgives␈αa␈αlist␈αobtained␈αfrom␈αthe␈αlist␈α␈↓↓u␈↓␈αby␈αinserting␈αsuch␈α␈↓¬B␈↓'s␈αas
␈↓ ↓H␈↓follows:
␈↓ ↓H␈↓2.3) ␈↓ α∞␈↓↓insertb u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬A ␈↓↓␈↓αthen␈↓↓ ␈↓¬A ␈↓↓. [␈↓¬B ␈↓↓. insertb ␈↓αd|␈↓↓u] ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . insertb ␈↓αd|␈↓↓u.␈↓
␈↓ ↓H␈↓Computing␈α␈↓↓insertb u␈↓␈αdoes␈αnot␈αchange␈αthe␈αvalue␈αof␈α␈↓↓u,␈↓␈αbecause␈αnew␈αlist␈αstructure␈αis␈αmanufactured.
␈↓ ↓H␈↓On the other hand, if we define
␈↓ ↓H␈↓↓␈↓ βxinsertb u ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[w];
␈↓ ↓H␈↓↓␈↓ ∧xw ← u;
␈↓ ↓H␈↓↓2.4)␈↓ ∧8loop:
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ u;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ ␈↓αa|␈↓↓w = ␈↓¬A ␈↓↓␈↓αthen␈↓↓ ␈↓αd|␈↓↓w ← ␈↓¬B ␈↓↓. ␈↓αd|␈↓↓w;
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓αdd|␈↓↓w;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ loop],
␈↓ ↓H␈↓we␈αget␈α
a␈αprogram␈α
that␈αdoes␈α
the␈αactual␈α
insertions.␈α It␈αis␈α
faster,␈αbecause␈α
it␈αuses␈α
fewer␈α␈↓↓cons␈↓es,␈α
but␈αit
␈↓ ↓H␈↓will␈α⊗damage␈α↔any␈α⊗list␈α⊗structure␈α↔that␈α⊗merges␈α⊗with␈α↔the␈α⊗structure␈α⊗representing␈α↔␈↓↓u,␈↓␈α⊗and␈α↔it␈α⊗is
␈↓ ↓H␈↓mathematically recalcitrant.
␈↓ ↓H␈↓3. ␈↓αRe-entrant List Structure␈↓
␈↓ ↓H␈↓ So␈α
far␈αwe␈α
have␈α
only␈αconsidered␈α
list␈α
structure␈αin␈α
which␈α
all␈α␈↓↓car-cdr␈↓␈α
chains␈α
terminate␈αin␈α
atoms.
␈↓ ↓H␈↓Mathematically␈αinfinite␈αlist␈αstructures␈αare␈αalso␈αpossible.␈α They␈αcan't␈αbe␈αrepresented␈αin␈αa␈αcomputer,
␈↓ ↓H␈↓but␈αsince␈αthey␈αcan␈αsatisfy␈αthe␈αLISP␈αalgebraic␈αaxioms,␈αwe␈αhave␈αhad␈αto␈αexclude␈αthem␈αin␈αour␈αproofs
␈↓ ↓H␈↓by␈α
axiom␈α
schemata␈α
of␈αinduction.␈α
Another␈α
possibility␈α
is␈α
the␈αre-entrant␈α
list␈α
structure.␈α
These␈αcan␈α
be
␈↓ ↓H␈↓created␈αby␈α
the␈α␈↓¬RPLACA␈α␈↓and␈α
␈↓¬RPLACD␈α␈↓operations.␈α Figure␈α
8␈αshows␈αsome␈α
examples␈αof␈α
re-entrant␈αlist
␈↓ ↓H␈↓structures.␈α For␈αexample,␈αif␈αthe␈αvariable␈α␈↓↓x␈↓␈αhas␈αvalue␈α␈↓¬(A),␈α␈↓executing␈αthe␈αstatement␈α␈↓↓␈↓αa|␈↓↓x␈α←␈αx␈↓␈αgives␈αrise
␈↓ ↓H␈↓to␈α
a␈α
circular␈αstructure␈α
(i)␈α
while␈αif␈α
we␈α
execute␈α␈↓↓␈↓αd|␈↓↓x␈α
←␈α
x␈↓␈αwe␈α
get␈α
the␈αstructure␈α
(ii).␈α
If␈αwe␈α
execute␈α
␈↓↓␈↓αd|␈↓↓x␈α←␈α
x␈↓
␈↓ ↓H␈↓with␈α
␈↓↓x␈↓␈αas␈α
in␈α(i)␈α
or␈α
␈↓↓␈↓αa|␈↓↓x␈α←␈α
x␈↓␈αwith␈α
␈↓↓x␈↓␈α
as␈αin␈α
(ii)␈αwe␈α
get␈α
the␈αdoubly␈α
re-entrant␈αstructure␈α
(iii).␈α Notice␈α
that
␈↓ ↓H␈↓in (i) we have ␈↓↓x ␈↓αeq␈↓↓ ␈↓αa|␈↓↓x␈↓, in (ii) ␈↓↓x ␈↓αeq␈↓↓ ␈↓αd|␈↓↓x␈↓ and in (iii) we have ␈↓↓x ␈↓αeq␈↓↓ ␈↓αa|␈↓↓x␈↓ and ␈↓↓x ␈↓αeq␈↓↓ ␈↓αd|␈↓↓x␈↓. If the statements
␈↓ ↓H␈↓␈↓ βP␈↓↓foo ← ␈↓¬(COND ((ATOM X) X) (T ((LAMBDA (X) BODY) (CAR X))))␈↓↓␈↓
␈↓ ↓H␈↓followed by
␈↓ ↓H␈↓␈↓ ¬"␈↓↓␈↓αa|␈↓↓␈↓αddaa|␈↓↓␈↓αdadd|␈↓↓foo ← foo ␈↓
␈↓ ↓H␈↓␈↓ ¬wChapter IV␈↓ *87
␈↓ ↓H␈↓are␈α→executed␈α~then␈α→␈↓↓foo␈↓␈α→will␈α~have␈α→the␈α→structure␈α~shown␈α→in␈α→figure␈α~8␈α→(iv).␈α~ Observe␈α→that
␈↓ ↓H␈↓␈↓↓eval[foo] = left[x]␈↓ where
␈↓ ↓H␈↓3.1) ␈↓ ∧l␈↓↓left x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ left ␈↓αa|␈↓↓x␈↓.
␈↓ ↓H␈↓If␈α∞the␈α∂reader␈α∞wishes␈α∞to␈α∂construct␈α∞re-entrant␈α∞list␈α∂structures␈α∞in␈α∞LISP,␈α∂this␈α∞had␈α∞best␈α∂be␈α∞done␈α∂in␈α∞a
␈↓ ↓H␈↓context␈αwhere␈αthe␈αresult␈αis␈αnot␈αprinted␈α(e.g.␈αreturned␈αas␈αa␈αvalue)␈αsince␈αif␈αLISP␈αattempts␈αto␈αprint␈αa
␈↓ ↓H␈↓re-entrant␈αlist␈αstructure␈αit␈αwill␈αnever␈αfinish.␈α One␈αway␈αto␈αget␈αaround␈αthis␈αis␈αto␈αdo␈αthe␈αconstruction
␈↓ ↓H␈↓inside a ␈↓¬PROG. ␈↓
␈↓"
␈↓"
␈↓"␈↓ ↓H␈↓ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ααααα→~ ~ ~ ααααα→~ ~ ~ αααααααα→~ ~ ~
␈↓"␈↓ ↓H␈↓ ↑ %απα∀απα$ ↑ %απα∀απα$ ↑ ↑ %απα∀απα$
␈↓"␈↓ ↓H␈↓ ~ ~ ↓ ~ ↓ ~ ~ ~ ~ ~
␈↓"␈↓ ↓H␈↓ %ααααα$ B ~ A ~ ~ %ααααα$ ~
␈↓"␈↓ ↓H␈↓ %ααααααααα$ %αααααααααααα$
␈↓"
␈↓"
␈↓"␈↓ ↓H␈↓ (RPLACA X X) (RPLACD X X) (RPLACA (RPLACDA X X))
␈↓"
␈↓"␈↓ ↓H␈↓ (i) (ii) (iii)
␈↓"
␈↓"
␈↓"
␈↓"
␈↓"␈↓ ↓H␈↓ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ααααα→~ ~ εαα→~ ~ εαααααα→~ ~ εαα→NIL
␈↓"␈↓ ↓H␈↓ ↑ %απα∀ααα$ %απα∀ααα$ %απα∀ααα$
␈↓"␈↓ ↓H␈↓ ~ ↓ ~ ↓
␈↓"␈↓ ↓H␈↓ ~ COND ~ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ~ ~ ~ ~ εαα→~ ~ εαα→NIL
␈↓"␈↓ ↓H␈↓ ~ ~ %απα∀ααα$ %απα∀ααα$
␈↓"␈↓ ↓H␈↓ ~ ~ ↓ ↓
␈↓"␈↓ ↓H␈↓ ~ ~ T ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ~ ↓ ~ ~ εαα→~ ~ εαα→NIL
␈↓"␈↓ ↓H␈↓ ~ ⊂αααπααα⊃ ⊂αααπααα⊃ %απα∀ααα$ %απα∀ααα$
␈↓"␈↓ ↓H␈↓ ~ ~ ~ εαα→~ ~ ~ ~ ↓
␈↓"␈↓ ↓H␈↓ ~ %απα∀ααα$ %απα∀απα$ ~ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ~ ~ ↓ ↓ ~ ~ ~ εαα→~ ~ ~
␈↓"␈↓ ↓H␈↓ ~ ~ X NIL ~ %απα∀ααα$ %απα∀απα$
␈↓"␈↓ ↓H␈↓ ~ ↓ ~ ↓ ↓ ↓
␈↓"␈↓ ↓H␈↓ ~ ⊂αααπααα⊃ ⊂αααπααα⊃ ~ CAR X NIL
␈↓"␈↓ ↓H␈↓ ~ ~ ~ εαα→~ ~ ~ ↓
␈↓"␈↓ ↓H␈↓ ~ %απα∀ααα$ %απα∀απα$ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ~ ↓ ↓ ↓ ~ ~ εαα→~ ~ εαα→~ ~ ~
␈↓"␈↓ ↓H␈↓ ~ ATOM X NIL %απα∀ααα$ %απα∀ααα$ %απα∀απα$
␈↓"␈↓ ↓H␈↓ ~ ↓ ↓ ~ ↓
␈↓"␈↓ ↓H␈↓ ~ LAMBDA ⊂αααπααα⊃ ~ NIL
␈↓"␈↓ ↓H␈↓ ~ ~ ~ ~ ~
␈↓"␈↓ ↓H␈↓ ~ %απα∀απα$ ~
␈↓"␈↓ ↓H␈↓ ~ ↓ ↓ ~
␈↓"␈↓ ↓H␈↓ ~ X NIL ~
␈↓"␈↓ ↓H␈↓ ~ ~
␈↓"␈↓ ↓H␈↓ %ααααααααααααααααααααααααααααααααααααααααααααααααααααααααααααααααααααα$
␈↓"
␈↓"
␈↓"␈↓ ↓H␈↓ (SETQ FOO (QUOTE (COND ((ATOM X) X) (T ((LAMVDA (X) BODY) (CAR X))))))
␈↓"
␈↓"␈↓ ↓H␈↓ (RPLACA (CDDAAR (CDADDR FOO)) FOO)
␈↓"
␈↓"␈↓ ↓H␈↓ (iv)
␈↓"
␈↓ ↓H␈↓␈↓ ε␈↓Figure 8.␈↓
␈↓ ↓H␈↓88␈↓ ¬wChapter IV␈↓ H
␈↓ ↓H␈↓␈↓ ¬wChapter IV␈↓ *89
␈↓ ↓H␈↓ As␈α
we␈αhave␈α
just␈αnoted,␈α
re-entrant␈αlist␈α
structures␈αdo␈α
not␈αsatisfy␈α
the␈αinduction␈α
axioms␈αgiven
␈↓ ↓H␈↓for␈α
S-expressions,␈α
and␈α
the␈α
correctness␈α
of␈α
most␈α
of␈α
programs␈α
given␈α
in␈α
this␈α
book␈α
depends␈α
on␈αthe␈α
data
␈↓ ↓H␈↓being␈α
non-re-entrant.␈α
Re-entrant␈α
structures␈αcan␈α
be␈α
represented␈α
by␈α
S-expressions␈αprovided␈α
certain
␈↓ ↓H␈↓atoms␈α⊃are␈α⊂reserved␈α⊃for␈α⊃use␈α⊂as␈α⊃labels␈α⊃and␈α⊂a␈α⊃suitable␈α⊃convention␈α⊂is␈α⊃adopted␈α⊃for␈α⊂distinguishing
␈↓ ↓H␈↓ordinary␈αatoms␈αfrom␈αlabels␈αand␈αpointers␈αto␈αthe␈αlabels.␈α For␈αexample,␈αthe␈αstructure␈αof␈αfigure␈α8␈α(iii)
␈↓ ↓H␈↓might␈αbe␈αrepresented␈αby␈α␈↓¬((LABEL A).((POINT A).(POINT A)))␈↓.␈α Programs␈αthat␈αcompute␈αwith␈αre-entrant
␈↓ ↓H␈↓structures␈αneed␈αto␈α
keep␈αlists␈αof␈αvertices␈α
they␈αhave␈αvisited␈α
so␈αthat␈αthey␈αcan␈α
tell␈αwhen␈αthey␈αare␈α
about
␈↓ ↓H␈↓to␈α
re-enter.␈α
Thus␈α
the␈α∞equivalence␈α
of␈α
merging␈α
list␈α
structure␈α∞can␈α
be␈α
tested␈α
by␈α
the␈α∞predicate␈α
␈↓↓equiv␈↓
␈↓ ↓H␈↓defined by:
␈↓ ↓H␈↓↓␈↓ β8equiv[x, y] ← ¬[equiv1[x, y, ␈↓¬NIL␈↓↓] = ␈↓¬LOSE␈↓↓]
␈↓ ↓H␈↓↓␈↓ β8equiv1[x, y, u] ←
␈↓ ↓H␈↓↓␈↓ βx␈↓αif␈↓↓ u = ␈↓¬LOSE␈↓↓ ␈↓αthen␈↓↓ ␈↓¬LOSE␈↓↓
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ x = y ∨ match[x, y, u] ␈↓αthen␈↓↓ u
␈↓ ↓H␈↓↓3.2)␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ∨ ␈↓αat|␈↓↓y ∨ unmatch[x, y, u] ␈↓αthen␈↓↓ ␈↓¬LOSE␈↓↓
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ equiv1[␈↓αa|␈↓↓x, ␈↓αa|␈↓↓y, equiv1[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y, [x . y] . u]]
␈↓ ↓H␈↓↓␈↓ β8match[x, y, u] ← ¬␈↓αn|␈↓↓u ∧ [[x = ␈↓αaa|␈↓↓u ∧ y = qdau] ∨ match[x, y, ␈↓αd|␈↓↓u]]
␈↓ ↓H␈↓↓␈↓ β8unmatch[x, y, u] ← ¬␈↓αn|␈↓↓u ∧ [x = ␈↓αaa|␈↓↓u ∨ y = qdau ∨ unmatch[x, y, ␈↓αd|␈↓↓u]]
␈↓ ↓H␈↓α␈↓ ε
Exercises
␈↓ ↓H␈↓ 1. Write a program to list the atoms in a possibly re-entrant list structure.
␈↓ ↓H␈↓ 2.␈αWrite␈αa␈αprogram␈αto␈αtranslate␈αa␈αgraph␈α
description␈αfrom␈αthe␈αnotation␈αof␈αChapter␈αI␈αto␈αa␈α
re-
␈↓ ↓H␈↓entrant list structure.
␈↓ ↓H␈↓ 3.␈αWrite␈α
functions␈αto␈α
translate␈αin␈αeach␈α
direction␈αbetween␈α
possibly␈αre-entrant␈α
structures␈αand
␈↓ ↓H␈↓the S-expressions corresponding to them using the ␈↓¬LABEL, ␈↓␈↓¬POINTER ␈↓notation described above.
␈↓ ↓H␈↓ 4.␈α
Write␈α
an␈α
axiom␈α
schema␈α
of␈α∞induction␈α
for␈α
finite␈α
possibly␈α
re-entrant␈α
list␈α∞structures.␈α
Hint:
␈↓ ↓H␈↓The␈αschema␈αcan␈αbe␈αbased␈αon␈αan␈αassertion␈αof␈αconvergence␈αof␈αa␈αprogram␈αthat␈αscans␈αa␈αlist␈αstructure
␈↓ ↓H␈↓keeping track of the vertices it has already visited.
␈↓ ↓H␈↓4. ␈↓αProperty Lists␈↓
␈↓ ↓H␈↓ It is necessary to associate at least the following kinds of information with atoms:
␈↓ ↓H␈↓ 1.␈α␈↓αPrint␈αnames.␈↓␈α Programs␈αthat␈αprint␈αlists␈αor␈αS-expressions␈αin␈αan␈αexternal␈αmedium␈αmust␈αbe
␈↓ ↓H␈↓able␈α
to␈α
find␈α
the␈α
string␈αof␈α
characters␈α
constituting␈α
the␈α
␈↓↓print␈α
name␈↓␈αof␈α
an␈α
atom.␈α
Likewise,␈α
when␈αan␈α
S-
␈↓ ↓H␈↓expression␈α
is␈α
read␈αfrom␈α
an␈α
external␈α
medium,␈αthe␈α
read␈α
program␈α
must␈αbe␈α
able␈α
to␈αdetermine␈α
whether
␈↓ ↓H␈↓there␈αis␈αalready␈αan␈αatom␈αwith␈αa␈αgiven␈αprint␈αname;␈αif␈αso␈αit␈αreturns␈αa␈αpointer␈αto␈αthat␈αatom;␈αif␈αnot␈αit
␈↓ ↓H␈↓creates a new atom with the given print name.
␈↓ ↓H␈↓90␈↓ ¬wChapter IV␈↓ H
␈↓ ↓H␈↓ 2.␈α
␈↓αValues.␈↓␈α When␈α
an␈α
atom␈αdenotes␈α
a␈α
variable␈αthe␈α
LISP␈αinterpreter␈α
will␈α
need␈αto␈α
be␈α
able␈αto
␈↓ ↓H␈↓look up the value associated with the atom in order to evaluate a term containing that variable.
␈↓ ↓H␈↓ 3.␈α␈↓αFunction␈αdefinitions.␈↓␈α In␈αthe␈αcase␈αthat␈αan␈αatom␈αdenotes␈αa␈αfunction,␈αthe␈αdefinition␈αof␈αthe
␈↓ ↓H␈↓function must be associated with the atom.
␈↓ ↓H␈↓ 4.␈α⊂␈↓αArray␈α∂pointers.␈↓␈α⊂ An␈α∂atom␈α⊂may␈α∂also␈α⊂denote␈α⊂an␈α∂array␈α⊂in␈α∂LISP.␈α⊂ In␈α∂this␈α⊂case␈α⊂an␈α∂array
␈↓ ↓H␈↓pointer or similar object must be associated with the atom in order for the array to be accessed.
␈↓ ↓H␈↓ LISP␈α
provides␈αa␈α
uniform␈αmanner␈α
for␈α
associating␈αinformation␈α
with␈αatoms,␈α
namely␈αeach␈α
atom
␈↓ ↓H␈↓has␈αa␈α␈↓↓property list.␈↓␈α LISP␈αuses␈αthe␈αproperty␈αlist␈αof␈αan␈αatom␈αto␈αstore␈αvarious␈αstandard␈αproperties.
␈↓ ↓H␈↓We␈α
will␈α∞discuss␈α
some␈α
particular␈α∞properties␈α
and␈α
manipulation␈α∞of␈α
properties␈α
below.␈α∞ Besides␈α
these
␈↓ ↓H␈↓standard␈α∀uses␈α∀of␈α∀property␈α∀lists,␈α∀it␈α∀is␈α∀convenient␈α∀to␈α∀allow␈α∀the␈α∀programmer␈α∀to␈α∀associate␈α∪any
␈↓ ↓H␈↓information␈αhe␈α
chooses␈αwith␈αan␈α
atom.␈α Such␈α
information␈αcan␈αbe␈α
retrieved␈αfor␈α
a␈αparticular␈αatom␈α
in
␈↓ ↓H␈↓a␈αtime␈αthat␈αdepends␈αon␈αthe␈αlength␈αof␈αthe␈αproperty␈αlist␈αof␈αthe␈αatom,␈αand␈αthis␈αis␈αusually␈αquite␈αshort.
␈↓ ↓H␈↓The time is independent of the number of atoms.
␈↓ ↓H␈↓ Property␈α⊂lists␈α∂are␈α⊂usually␈α⊂realized␈α∂as␈α⊂ordinary␈α⊂lists.␈α∂ Each␈α⊂"property"␈α⊂has␈α∂an␈α⊂atom␈α⊂as␈α∂its
␈↓ ↓H␈↓name,␈α∂and␈α∂the␈α∞name␈α∂of␈α∂the␈α∞property␈α∂is␈α∂ordinarily␈α∞followed␈α∂by␈α∂a␈α∞pointer␈α∂to␈α∂the␈α∂information␈α∞in
␈↓ ↓H␈↓question.␈α∞ Because␈α∞this␈α∞information␈α∞can␈α
be␈α∞a␈α∞string␈α∞of␈α∞characters,␈α
a␈α∞floating␈α∞point␈α∞number␈α∞or␈α
a
␈↓ ↓H␈↓pointer␈α
to␈αa␈α
subroutine,␈αthe␈α
property␈αlist␈α
is␈α
ordinarily␈αnot␈α
a␈αproper␈α
LISP␈αlist,␈α
and␈α
functions␈αthat
␈↓ ↓H␈↓operate␈αon␈αproper␈αlists␈αcan␈αcause␈αerrors␈αif␈αapplied␈αto␈αproperty␈αlists.␈α For␈αexample,␈αa␈αprogram␈αthat
␈↓ ↓H␈↓interpreted␈α
a␈α
floating␈α
point␈α
number␈α
as␈α
a␈α
pair␈α
of␈α
pointers␈α
interpret␈α
random␈α
places␈α
in␈α
memory␈αas
␈↓ ↓H␈↓list structure.
␈↓ ↓H␈↓ For␈α
this␈α
reason,␈α
property␈α
lists␈α
are␈α∞usually␈α
read␈α
and␈α
manipulated␈α
by␈α
special␈α∞functions.␈α
We
␈↓ ↓H␈↓have␈α⊂already␈α∂discussed␈α⊂␈↓¬DEFUN␈α∂␈↓and␈α⊂␈↓¬DEFPROP␈α∂which␈α⊂␈↓can␈α∂be␈α⊂used␈α∂to␈α⊂put␈α∂function␈α⊂definitions␈α∂on
␈↓ ↓H␈↓property lists. The three basic functions on property lists are:
␈↓ ↓H␈↓␈↓ ∧8(␈↓¬GET ␈↓<atom> <property>)
␈↓ ↓H␈↓␈↓ ∧8(␈↓¬PUTPROP ␈↓<atom> <value> <property>)
␈↓ ↓H␈↓␈↓ ∧8(␈↓¬REMPROP ␈↓<atom> <property>)
␈↓ ↓H␈↓where␈α<atom>␈αshould␈αevaluate␈αto␈αan␈αatom,␈α<property>␈αmust␈αevaluate␈αto␈αan␈αatom␈αwhich␈αnames␈α
the
␈↓ ↓H␈↓property,␈α∂and␈α⊂<value>␈α∂can␈α⊂be␈α∂any␈α∂LISP␈α⊂expression␈α∂that␈α⊂can␈α∂be␈α∂evaluated.␈α⊂ [In␈α∂some␈α⊂LISPs␈α∂a
␈↓ ↓H␈↓property list is also an accepted value for <atom>.]
␈↓ ↓H␈↓ ␈↓¬GET␈α␈↓returns␈α
the␈α<property>␈αvalue␈α
(if␈αany)␈α
associated␈αwith␈α<atom>.␈α
␈↓¬PUTPROP␈α␈↓puts␈αa␈α
property
␈↓ ↓H␈↓name␈α∂<property>␈α⊂followed␈α∂by␈α⊂the␈α∂property␈α⊂value␈α∂<value>␈α∂on␈α⊂the␈α∂property␈α⊂list␈α∂of␈α⊂<atom>,␈α∂and
␈↓ ↓H␈↓␈↓¬REMPROP␈α∩␈↓removes␈α∪a␈α∩property.␈α∪ ␈↓¬DEFPROP␈α∩␈↓is␈α∪like␈α∩␈↓¬PUTPROP␈α∪␈↓except␈α∩that␈α∪the␈α∩arguments␈α∪are␈α∩not
␈↓ ↓H␈↓evaluated.␈α∞ Thus␈α
if␈α∞␈↓¬(PUTPROP 'C 12 'AT-WT)␈↓␈α
is␈α∞executed␈α
then␈α∞␈↓¬(GET 'C 'AT-WT)␈↓␈α
will␈α∞return␈α
␈↓¬12.␈α∞␈↓␈α
If
␈↓ ↓H␈↓␈↓¬(REMPROP 'C 'AT-WT)␈↓␈αis␈αexecuted␈αthen␈α
␈↓¬(GET 'C 'AT-WT)␈↓␈αwill␈αreturn␈α␈↓¬NIL␈↓␈α
as␈αthe␈α␈↓¬AT-WT␈α␈↓property␈αis␈α
no
␈↓ ↓H␈↓longer on the property list of ␈↓¬C. ␈↓ The exact form of these functions is implementation dependent.
␈↓ ↓H␈↓ Most␈αLISPs␈αallow␈αyou␈αto␈αexamine␈αthe␈αproperty␈αlist␈αof␈αan␈αatom.␈α It␈αis␈αtypically␈αstored␈αas␈αthe
␈↓ ↓H␈↓␈↓↓cdr␈↓␈α∞of␈α∞the␈α∞atom␈α∞so␈α∞(␈↓¬CDR␈α∞␈↓<atom>)␈α∞returns␈α∞the␈α∞property␈α∞list␈α∞of␈α∞<atom>.␈α∞ In␈α∞MACLISP␈α∞there␈α∞is␈α∞a
␈↓ ↓H␈↓special function ␈↓¬PLIST ␈↓for doing this. Thus (␈↓¬PLIST ␈↓<atom>) returns the property list of <atom>.
␈↓ ↓H␈↓ From␈α∀a␈α∀mathematical␈α∃point␈α∀of␈α∀view␈α∀␈↓¬PUTPROP␈α∃␈↓acts␈α∀like␈α∀an␈α∀assignment␈α∃statement␈α∀and
␈↓ ↓H␈↓␈↓ ¬wChapter IV␈↓ *91
␈↓ ↓H␈↓␈↓¬REMPROP␈α∩␈↓like␈α⊃a␈α∩special␈α⊃kind␈α∩of␈α⊃an␈α∩assignment␈α⊃statement.␈α∩ ␈↓¬GET␈α⊃␈↓gets␈α∩the␈α⊃value␈α∩of␈α∩a␈α⊃variable.
␈↓ ↓H␈↓However,␈α∀the␈α∀mathematical␈α∀properties␈α∀of␈α∀these␈α∀LISP␈α∀operations␈α∀haven't␈α∃been␈α∀systematically
␈↓ ↓H␈↓studied.
␈↓ ↓H␈↓ The␈α
particular␈α
collection␈α
of␈α
standard␈α
properties␈α
stored␈α
by␈α
LISP␈α
on␈α
the␈α
property␈α
list␈α∞of␈α
an
␈↓ ↓H␈↓atom␈α⊃depends␈α⊃on␈α⊃the␈α⊂implementation.␈α⊃ Some␈α⊃LISPs␈α⊃store␈α⊂␈↓¬VALUE␈α⊃␈↓and␈α⊃␈↓¬PNAME␈α⊃␈↓properties␈α⊂there,
␈↓ ↓H␈↓others␈α∞(MACLISP␈α
in␈α∞particular)␈α
store␈α∞these␈α
properties␈α∞in␈α
some␈α∞"hidden"␈α
place.␈α∞ Properties␈α
that
␈↓ ↓H␈↓are␈αkept␈αon␈αthe␈αproperty␈αlist␈αby␈αmost␈αLISPs␈αinclude␈α␈↓¬EXPR,␈α␈↓␈↓¬FEXPR,␈α␈↓␈↓¬LEXPR,␈α␈↓␈↓¬SUBR,␈α␈↓␈↓¬FSUBR,␈α␈↓␈↓¬LSUBR,␈α
␈↓and
␈↓ ↓H␈↓␈↓¬ARRAY. ␈↓
␈↓ ↓H␈↓ An␈α∞atom␈α∞with␈α∞an␈α∞␈↓¬EXPR␈α∞␈↓property␈α∞is␈α∞assumed␈α∞by␈α∞LISP␈α∞to␈α∞denote␈α∞an␈α∂interpretable␈α∞function
␈↓ ↓H␈↓and␈αthe␈αvalue␈αof␈αthe␈α␈↓¬EXPR␈α␈↓property␈αshould␈αbe␈αa␈αfunction␈αexpression␈α(such␈αas␈αa␈αfunction␈α
name,␈αa
␈↓ ↓H␈↓λ-expression,␈αor␈αa␈αlabel-expression)␈αsuitable␈αfor␈αbeing␈αinterpreted.␈α It␈αcan␈αbe␈αset␈αusing␈αone␈αof␈αthe
␈↓ ↓H␈↓operations␈α␈↓¬DEFUN␈α␈↓or␈α␈↓¬DEFPROP␈α␈↓␈αdescribed␈αin␈αChapter␈αI.␈α An␈αatom␈αwith␈αa␈α␈↓¬SUBR␈α␈↓property␈αdenotes␈αa
␈↓ ↓H␈↓compiled␈α∀function␈α∀which␈α∪may␈α∀be␈α∀directly␈α∀executed.␈α∪ The␈α∀value␈α∀of␈α∪the␈α∀␈↓¬SUBR␈α∀␈↓property␈α∀is␈α∪a
␈↓ ↓H␈↓subroutine␈α
pointer␈α
which␈α
points␈αto␈α
the␈α
compiled␈α
program.␈α Atoms␈α
denoting␈α
built␈α
in␈αfunctions␈α
such
␈↓ ↓H␈↓as␈α␈↓¬CAR␈α␈↓or␈α␈↓¬APPEND␈α␈↓have␈αa␈α␈↓¬SUBR␈α␈↓property␈αautomatically.␈α When␈αa␈αcompiled␈αfunction␈αis␈αloaded␈αinto
␈↓ ↓H␈↓LISP␈α⊂the␈α⊂name␈α⊂of␈α⊂that␈α⊂function␈α⊂gets␈α∂a␈α⊂␈↓¬SUBR␈α⊂␈↓property.␈α⊂ The␈α⊂properties␈α⊂␈↓¬FEXPR␈α⊂␈↓and␈α⊂␈↓¬LEXPR␈α∂are
␈↓ ↓H␈↓¬␈↓similar␈αto␈α
␈↓¬EXPR␈α␈↓except␈α
that␈αthe␈α
arguments␈αto␈αthe␈α
function␈αare␈α
treated␈αdifferently.␈α
In␈αthe␈α␈↓¬EXPR␈α
␈↓case
␈↓ ↓H␈↓a␈α
fixed␈αnumber␈α
of␈αarguments␈α
are␈αexpected␈α
and␈αthey␈α
are␈αevaluated␈α
before␈αapplying␈α
the␈αfunction.
␈↓ ↓H␈↓In␈αthe␈α␈↓¬FEXPR␈α␈↓case␈αthe␈αlist␈αof␈αarguments␈αis␈αpassed␈αunevaluated␈αto␈αthe␈αfunction␈α(as␈αa␈α
single␈αentity).
␈↓ ↓H␈↓In␈αthe␈α␈↓¬LEXPR␈α␈↓case␈αthe␈αnumber␈αof␈αarguments␈αis␈αarbitrary,␈α the␈αarguments␈αare␈αevaluated␈αand␈αsaved,
␈↓ ↓H␈↓the␈αnumber␈αof␈αarguments␈αis␈αpassed␈αto␈αthe␈αfunction␈αand␈αthe␈αfunction␈αmust␈αuse␈αa␈αspecial␈αauxiliary
␈↓ ↓H␈↓function to access the argument values. The three ␈↓¬SUBR ␈↓cases are analogous to those for ␈↓¬EXPR. ␈↓
␈↓ ↓H␈↓ An␈αatom␈α
with␈αan␈α
␈↓¬ARRAY␈α␈↓property␈α
denotes␈αan␈α
array.␈α The␈α
value␈αof␈α
the␈α␈↓¬ARRAY␈α
␈↓property␈αis␈α
an
␈↓ ↓H␈↓array␈αpointer␈αwhich␈αprovides␈αLISP␈αwith␈αa␈αmeans␈αof␈αaccessing␈αthe␈αarray.␈α The␈αarray␈αproperty␈αcan
␈↓ ↓H␈↓be␈α
set␈α
by␈α
executing␈α
a␈α
statement␈α
of␈α
the␈α
form␈α
(␈↓¬ARRAY␈α
␈↓<name>␈α
<type>␈α
<b1>␈α
...␈α
<bn>).␈α∞ This␈α
causes
␈↓ ↓H␈↓LISP␈αto␈αallocate␈αspace␈α
for␈αan␈αn-diminsional␈αarray␈αof␈α
type␈αgiven␈αby␈α<type>␈αhaving␈α
bounds␈αgiven
␈↓ ↓H␈↓by␈α∞the␈α∞<bi>'s␈α∞ and␈α∞to␈α∞set␈α∞the␈α∞␈↓¬ARRAY␈α∞␈↓property␈α∞of␈α∞<name>.␈α∞ The␈α∞value␈α∞of␈α∞the␈α∂assigned␈α∞property
␈↓ ↓H␈↓includes type, bounds, and location information.
␈↓ ↓H␈↓ A␈α∩major␈α∩use␈α∩of␈α⊃property␈α∩lists␈α∩in␈α∩programming␈α∩is␈α⊃in␈α∩the␈α∩creation␈α∩and␈α∩modification␈α⊃of
␈↓ ↓H␈↓networks␈α∂of␈α∂data.␈α∂ Values␈α∂attached␈α∂to␈α∂atoms␈α∂point␈α∂to␈α∂expressions␈α∂that␈α∂include␈α∂other␈α⊂atoms.␈α∂ A
␈↓ ↓H␈↓program␈α
manipulating␈α
chemical␈α
structures␈α
might␈α
use␈α
property␈α
lists␈α
to␈α
associate␈α
such␈α
information␈α
as
␈↓ ↓H␈↓valence,␈αatomic␈αweight,␈αnuclear␈αspin,␈αetc.␈α with␈αeach␈αchemical␈αatom␈αof␈αinterest.␈α It␈αcould␈αalso␈αtreat
␈↓ ↓H␈↓fragments␈αas␈αquasi␈αatoms␈αby␈αnaming␈αthem,␈αputting␈αthe␈αstructure␈αof␈αthe␈αfragment␈αon␈αthe␈αproperty
␈↓ ↓H␈↓list,␈αas␈αwell␈αas␈αanalogues␈αto␈αthe␈αatomic␈αproperties.␈α Another␈αexample␈αis␈αthe␈αprogram␈αused␈αto␈αprint
␈↓ ↓H␈↓the␈α∞LISP␈α
programs␈α∞appearing␈α
in␈α∞this␈α
book␈α∞in␈α
external␈α∞form.␈α
Each␈α∞special␈α
LISP␈α∞atom␈α∞(such␈α
as
␈↓ ↓H␈↓␈↓¬CAR,␈α
␈↓␈↓¬COND,␈α
␈↓␈↓¬APPEND,␈α␈↓␈↓¬QUOTE,␈α
␈↓...)␈α
has␈αon␈α
its␈α
property␈α
list␈αthe␈α
name␈α
of␈αthe␈α
function␈α
used␈α
to␈αcompile
␈↓ ↓H␈↓the␈αconstruct␈αit␈αsignals,␈αtype␈αinformation,␈αexternal␈αprint␈αname␈α(so␈αthat␈α␈↓¬CAR␈α␈↓prins␈αas␈α␈↓αa|␈↓for␈αexample)
␈↓ ↓H␈↓and other control information.
␈↓ ↓H␈↓5. ␈↓αInput and output.␈↓
␈↓ ↓H␈↓ A␈αbasic␈αcomponent␈αof␈αany␈αLISP␈αsystem␈αis␈α
the␈αcollection␈αof␈αprograms␈αthat␈αread␈αand␈αprint␈α
S-
␈↓ ↓H␈↓expressions.␈α∂ A␈α∞read␈α∂program␈α∞reads␈α∂an␈α∞S-expression␈α∂in␈α∞some␈α∂external␈α∞notation␈α∂and␈α∂converts␈α∞it
␈↓ ↓H␈↓92␈↓ ¬wChapter IV␈↓ H
␈↓ ↓H␈↓into␈α⊂internal␈α∂form␈α⊂(list␈α∂structures).␈α⊂ A␈α⊂print␈α∂program␈α⊂converts␈α∂the␈α⊂internal␈α∂form␈α⊂back␈α⊂into␈α∂the
␈↓ ↓H␈↓external␈αrepresentation.␈α The␈αrecursive␈αstructure␈αof␈αthe␈αread␈αand␈αprint␈αprograms␈αcan␈αbe␈αdescribed
␈↓ ↓H␈↓by␈α
choosing␈α
a␈α
particular␈α
external␈α
representation.␈α Namely␈α
we␈α
represent␈α
the␈α
linear␈α
string␈αof␈α
symbols
␈↓ ↓H␈↓used␈αto␈αwrite␈αdown␈αS-expressions␈αby␈αa␈αlist␈αof␈αatoms␈αrepresenting␈αthe␈αsymbols␈αin␈αthe␈αstring.␈α The
␈↓ ↓H␈↓symbols␈α"(",␈α
")"␈αand␈α
"."␈αare␈α
represented␈αby␈α
the␈αatoms␈α
␈↓¬LP,␈α␈↓␈↓¬RP,␈α
␈↓and␈α␈↓¬DOT␈α
␈↓respectively.␈α The␈αprint␈α
name
␈↓ ↓H␈↓of an atom is represented by the corresponding atom.
␈↓ ↓H␈↓ The program ␈↓↓prina␈↓ "print"s an S-expression in "dot" notation. The definition of ␈↓↓prina␈↓ is:
␈↓ ↓H␈↓↓␈↓ β8prina[e, l] ←
␈↓ ↓H␈↓↓5.1)␈↓ βx␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ e . l
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ ␈↓¬LP␈↓↓ . prina[␈↓αa|␈↓↓e, ␈↓¬DOT␈↓↓ . prina[␈↓αd|␈↓↓e, ␈↓¬RP␈↓↓ . l]]
␈↓ ↓H␈↓thus
␈↓ ↓H␈↓␈↓ β8␈↓↓prina[␈↓¬(PLUS (TIMES A B) C), NIL␈↓↓] = ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓¬(LP PLUS DOT LP LP TIMES DOT LP A DOT LP B ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓¬ DOT NIL RP RP RP DOT LP C DOT NIL RP RP RP),␈↓
␈↓ ↓H␈↓ The␈αprogram␈α␈↓↓prinb␈↓␈α"print"s␈αan␈αS-expression␈αin␈α"list"␈αnotation.␈α In␈αthe␈αnon-atomic␈αcase,␈αthis
␈↓ ↓H␈↓is␈αdone␈α by␈αstarting␈αwith␈α␈↓¬LP,␈α␈↓␈α␈↓↓cdr␈↓ing␈αthrough␈αthe␈αS-expression␈αprinting␈αthe␈α␈↓↓car␈↓␈αat␈αeach␈αlevel␈αuntil
␈↓ ↓H␈↓an␈α⊂atom␈α⊂is␈α∂reached.␈α⊂ If␈α⊂this␈α∂atom␈α⊂is␈α⊂␈↓¬NIL␈↓␈α∂the␈α⊂S-expression␈α⊂is␈α∂indeed␈α⊂a␈α⊂list␈α∂and␈α⊂the␈α⊂printing␈α∂is
␈↓ ↓H␈↓terminated␈αwith␈α␈↓¬RP.␈α␈↓Otherwise␈αthe␈αprinting␈αis␈αterminated␈αwith␈α␈↓¬DOT␈α␈↓followed␈αby␈αthe␈αatom␈αfollowed
␈↓ ↓H␈↓by ␈↓¬RP. ␈↓ ␈↓↓prinb␈↓ is defined by:
␈↓ ↓H␈↓↓␈↓ β8prinb[e, l] ←
␈↓ ↓H␈↓↓␈↓ βx␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ e . l
␈↓ ↓H␈↓↓5.2)␈↓ βx␈↓αelse␈↓↓ ␈↓¬LP␈↓↓ . [␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓e ␈↓αthen␈↓↓ prinb[␈↓αa|␈↓↓e, ␈↓¬RP␈↓↓ . l]
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αd|␈↓↓e ␈↓αthen␈↓↓ prinb[␈↓αa|␈↓↓e, ␈↓¬DOT␈↓↓ . [␈↓αd|␈↓↓e . [␈↓¬RP␈↓↓ . l]]]
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αelse␈↓↓ prinb[␈↓αa|␈↓↓e, ␈↓αd|␈↓↓prinb[␈↓αd|␈↓↓e, l]]]
␈↓ ↓H␈↓thus
␈↓ ↓H␈↓␈↓ β~␈↓↓prinb[␈↓¬(PLUS (TIMES A B) C), NIL␈↓↓] = ␈↓¬(LP PLUS LP TIMES A B RP C RP).␈↓↓␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ∧β␈↓↓ prinb[␈↓¬(A . ( B . (C . D)))␈↓↓] = ␈↓¬(LP A B C DOT D RP).␈↓↓␈↓
␈↓ ↓H␈↓ The␈αprogram␈α␈↓↓read␈↓␈αconverts␈α
a␈αlist␈αof␈αsymbols␈α
representing␈αan␈αS-expression␈αin␈α"dot"␈α
notation,
␈↓ ↓H␈↓or␈α∞"list"␈α∂notation␈α∞or␈α∞some␈α∂mixture␈α∞thereof␈α∞into␈α∂the␈α∞corresponding␈α∞S-expression.␈α∂ The␈α∞auxiliary
␈↓ ↓H␈↓function ␈↓↓reada␈↓ uses the auxiliary variable ␈↓↓l␈↓ to stack partial results. The definitions are as follows:
␈↓ ↓H␈↓␈↓ ¬wChapter IV␈↓ *93
␈↓ ↓H␈↓↓␈↓ β8read u ← ␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬LP␈↓↓ ␈↓αthen␈↓↓ ␈↓αa|␈↓↓reada[␈↓αd|␈↓↓u, ␈↓¬NIL␈↓↓] ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u
␈↓ ↓H␈↓↓␈↓ β8reada[u, l] ←
␈↓ ↓H␈↓↓␈↓ βx␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ rev1[l, ␈↓¬ERROR␈↓↓] . ␈↓¬NIL␈↓↓
␈↓ ↓H␈↓↓5.3)␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬RP␈↓↓ ␈↓αthen␈↓↓ reverse l . ␈↓αd|␈↓↓u
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬LP␈↓↓ ␈↓αthen␈↓↓ {reada[␈↓αd|␈↓↓u, ␈↓¬NIL␈↓↓]}[λw: reada[␈↓αd|␈↓↓w, ␈↓αa|␈↓↓w . l]]
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬DOT␈↓↓ ␈↓αthen␈↓↓ {reada[␈↓αd|␈↓↓u, ␈↓¬NIL␈↓↓]}[λw: rev1[l, ␈↓αaa|␈↓↓w] . ␈↓αd|␈↓↓w]
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ reada[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u . l]
␈↓ ↓H␈↓↓␈↓ β8rev1[u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ rev1[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u . v]
␈↓ ↓H␈↓ ␈↓↓read␈↓␈α∪and␈α∀␈↓↓prina␈↓␈α∪or␈α∀␈↓↓prinb␈↓␈α∪are␈α∀inverses␈α∪of␈α∀one␈α∪another␈α∀modulo␈α∪conversion␈α∀of␈α∪external
␈↓ ↓H␈↓representations.␈α→ Thus␈α~␈↓↓read[prina[e, ␈↓¬NIL␈↓↓]] = e␈↓,␈α→␈↓↓read[prinb[e, ␈↓¬NIL␈↓↓]] = e␈↓,␈α~and␈α→if␈α~␈↓↓u␈↓␈α→is␈α~a␈α→"dot"
␈↓ ↓H␈↓representation␈α≡then␈α≥␈↓↓prina[read[u], ␈↓¬NIL␈↓↓] = u␈↓,␈α≡while␈α≡if␈α≥␈↓↓u␈↓␈α≡is␈α≥a␈α≡"list"␈α≡representation␈α≥then
␈↓ ↓H␈↓␈↓↓prinb[read[u], ␈↓¬NIL␈↓↓] = u␈↓.
␈↓ ↓H␈↓ We␈α∂note␈α∂in␈α∞passing␈α∂that␈α∂the␈α∂actual␈α∞LISP␈α∂reading␈α∂programs␈α∂must␈α∞deal␈α∂with␈α∂the␈α∂oblist␈α∞in
␈↓ ↓H␈↓order␈α
to␈α
identify␈αthe␈α
atom␈α
having␈αa␈α
given␈α
print␈α
name,␈αpossibly␈α
updating␈α
the␈αoblist␈α
if␈α
the␈αatom␈α
has
␈↓ ↓H␈↓not␈α⊂been␈α⊂seen␈α⊃before␈α⊂and␈α⊂the␈α⊂printing␈α⊃programs␈α⊂must␈α⊂deal␈α⊂with␈α⊃property␈α⊂lists␈α⊂or␈α⊃some␈α⊂other
␈↓ ↓H␈↓mechanism for obtaining the print name of an atom.
␈↓ ↓H␈↓94␈↓ εH␈↓ H
␈↓ ↓H␈↓α␈↓ εαChapter V
␈↓ ↓H␈↓α␈↓ ∧CTRANSFORMING LISP PROGRAMS
␈↓ ↓H␈↓ The␈α∞techniques␈α
developed␈α∞in␈α
Chapter␈α∞III␈α∞were␈α
limited␈α∞to␈α
stating␈α∞and␈α∞proving␈α
extensional
␈↓ ↓H␈↓properties␈α⊂of␈α⊂functional␈α⊂LISP␈α⊂programs.␈α⊂ We␈α⊂have␈α⊂seen␈α⊂in␈α⊂Chapter␈α⊂IV␈α⊂that␈α⊂in␈α⊂general␈α⊂LISP
␈↓ ↓H␈↓programs␈α
may␈α
not␈α
be␈α
functional.␈α
Also␈α
many␈α
interesting␈α
properties␈α
of␈α
programs␈α
such␈α
as␈α∞space␈α
or
␈↓ ↓H␈↓time␈αrequirements,␈α
depth␈αof␈αrecursion,␈α
number␈αof␈α
calls␈αto␈αa␈α
particular␈αfunction␈α
during␈αexecution,
␈↓ ↓H␈↓etc.,␈αare␈αnot␈α
extensional.␈α In␈αthis␈α
chapter␈αwe␈αshow␈α
how␈αthe␈αfirst␈α
order␈αtechniques␈αof␈α
Chapter␈αIII
␈↓ ↓H␈↓can␈α
be␈α
applied␈α∞to␈α
a␈α
larger␈α∞class␈α
of␈α
problems.␈α∞ In␈α
particular␈α
we␈α∞discuss␈α
how␈α
to␈α∞transform␈α
certain
␈↓ ↓H␈↓sequential␈α∪LISP␈α∪programs␈α∪into␈α∪functional␈α∪programs␈α∪that␈α∪compute␈α∪the␈α∪same␈α∪function.␈α∩ Thus
␈↓ ↓H␈↓extensional␈αproperties␈α
can␈αbe␈αdiscussed␈α
in␈αterms␈α
of␈αthe␈αequivalent␈α
functional␈αprogram.␈α
We␈αalso
␈↓ ↓H␈↓show␈α⊃how␈α⊃some␈α⊃intensional␈α⊃properties␈α⊃can␈α⊃be␈α⊃handled␈α⊃by␈α⊃forming␈α⊃a␈α⊃"derived"␈α⊃program␈α⊃that
␈↓ ↓H␈↓computed␈αthe␈αdesired␈αproperty␈αof␈αthe␈αoriginal␈αprogram.␈α Finally␈αwe␈αdiscuss␈αbriefly␈αhow␈αto␈αconnect
␈↓ ↓H␈↓the␈αformal␈α
properties␈αof␈αtransforming␈α
programs␈αwith␈α
those␈αof␈αthe␈α
transformed␈αprograms␈αusing␈α
the
␈↓ ↓H␈↓␈↓↓meta␈↓␈α
theory␈αof␈α
LISP.␈α This␈α
connection␈αwill␈α
allow␈αus␈α
for␈αexample␈α
to␈αdefine␈α
intensional␈αproperties
␈↓ ↓H␈↓formally and show that the derived programs actually do compute the property correctly.
␈↓ ↓H␈↓1. ␈↓αPurifying LISP programs.␈↓
␈↓ ↓H␈↓ As␈αwe␈α
noted␈αin␈α
Chapter␈αIV␈α
a␈αsequential␈α
term␈αoccuring␈α
as␈αa␈α
term␈αin␈α
a␈αLISP␈α
program␈αcan␈α
be
␈↓ ↓H␈↓replaced␈αby␈αan␈αextensionally␈αequivalent␈αfunctional␈αterm␈αunder␈αcertain␈αconditions.␈α A␈αsufficient␈αset
␈↓ ↓H␈↓of conditions are the following:
␈↓ ↓H␈↓␈↓ αλ1.␈αAll␈αvariables␈αlocal␈α
to␈αthe␈αprogram␈αare␈α
initialized␈αbefore␈αbeing␈αused.␈α
Of␈αcourse,␈αit␈αcould␈α
be
␈↓ ↓H␈↓␈↓ αHtaken␈α∞as␈α∞a␈α∂convention␈α∞that␈α∞these␈α∂variables␈α∞are␈α∞initialized␈α∞to␈α∂␈↓¬NIL␈↓␈α∞when␈α∞the␈α∂program␈α∞is
␈↓ ↓H␈↓␈↓ αHentered.␈α∪ Then␈α∩it␈α∪wouldn't␈α∩affect␈α∪the␈α∪correctness␈α∩of␈α∪the␈α∩translation␈α∪to␈α∪a␈α∩functional
␈↓ ↓H␈↓␈↓ αHprogram if some of them weren't further initialized.
␈↓ ↓H␈↓␈↓ αλ2. All assignments are to variables local to the program.
␈↓ ↓H␈↓␈↓ αλ3. Each label referred to in a ␈↓αgo to␈↓ statement occurs exactly once in the program.
␈↓ ↓H␈↓ One␈α
method␈α
of␈αfinding␈α
a␈α
functional␈α
term␈αequivalent␈α
to␈α
a␈αsequential␈α
term␈α
is␈α
the␈αfollowing.
␈↓ ↓H␈↓Suppose␈α∀the␈α∃␈↓αprogram␈↓␈α∀has␈α∃local␈α∀variables␈α∀␈↓↓x1,...,xn.␈↓␈α∃ For␈α∀each␈α∃conditional␈α∀branch␈α∃(i.e.␈α∀a
␈↓ ↓H␈↓conditional␈α
statement␈αcontaining␈α
a␈α␈↓αgo to␈↓␈α
or␈αa␈α
␈↓αreturn␈↓)␈αwe␈α
define␈αa␈α
new␈αfunction.␈α
If␈αthe␈α
conditional
␈↓ ↓H␈↓has the form
␈↓ ↓H␈↓␈↓ ∧1␈↓↓␈↓αif␈↓↓ p1 ␈↓αthen␈↓↓ s1 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ... ␈↓αelse␈↓↓ ␈↓αif␈↓↓ pn ␈↓αthen␈↓↓ sn␈↓
␈↓ ↓H␈↓then the body of the function definition has the form
␈↓ ↓H␈↓␈↓ ∧¬␈↓↓␈↓αif␈↓↓ p1 ␈↓αthen␈↓↓ t1 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ... ␈↓αelse␈↓↓ ␈↓αif␈↓↓ pn ␈↓αthen␈↓↓ tn ␈↓αelse␈↓↓ t0␈↓
␈↓ ↓H␈↓where␈α␈↓↓s0␈↓␈α
is␈αthe␈αsequence␈α
of␈αstatements␈α
following␈αthe␈αconditional␈α
and␈α␈↓↓ti␈↓␈αis␈α
the␈αresult␈α
of␈αexecuting
␈↓ ↓H␈↓the␈αsequence␈αof␈α
statements␈α␈↓↓si;s0␈↓␈α(or␈αjust␈α
␈↓↓s0␈↓␈α for␈α␈↓↓t0␈↓␈α)␈α
taking␈αthe␈αcurrent␈αvalue␈α
of␈α␈↓↓xi␈↓␈αto␈αbe␈α
␈↓↓xi.␈↓␈α We
␈↓ ↓H␈↓␈↓ ¬|Chapter V␈↓ *95
␈↓ ↓H␈↓obtain␈α
the␈α
term␈α
corresponding␈α∞to␈α
the␈α
result␈α
of␈α
executing␈α∞a␈α
sequence␈α
of␈α
statements␈α
as␈α∞follows.␈α
If
␈↓ ↓H␈↓the␈αstatement␈αis␈αan␈αassigment␈αthen␈αthe␈αcurrent␈αvalue␈αof␈αthe␈αvariable␈αbecomes␈αthe␈αcurrent␈αvalue␈αof
␈↓ ↓H␈↓the␈α∂term␈α∂on␈α∂the␈α∞right␈α∂hand␈α∂side.␈α∂(The␈α∂current␈α∞value␈α∂of␈α∂the␈α∂term␈α∞is␈α∂just␈α∂the␈α∂term␈α∂obtained␈α∞by
␈↓ ↓H␈↓replacing␈α
all␈α
free␈α
occurrences␈α
of␈α
local␈α
variables␈α
by␈α
their␈α
current␈α
values.␈α
If␈α
some␈α
local␈α
variable␈α
is
␈↓ ↓H␈↓needed␈α
that␈α
has␈α
no␈α
current␈α
value␈α
then␈α
the␈α
current␈α
value␈α
of␈α
the␈α
term␈α
is␈α
undefined.)␈α
If␈α
the␈α
statement
␈↓ ↓H␈↓is␈αa␈αconditional␈αwithout␈αany␈α␈↓αgo to␈↓s␈αor␈α␈↓αreturn␈↓s␈αthen␈αit␈αgets␈αconverted␈αto␈αa␈αsequence␈αof␈αassignments
␈↓ ↓H␈↓where␈α∞the␈α
conditional␈α∞has␈α
been␈α∞distributed␈α∞through␈α
the␈α∞assignment.␈α
If␈α∞the␈α
statement␈α∞is␈α∞a␈α
␈↓αgo to␈↓
␈↓ ↓H␈↓then␈α∂the␈α∂next␈α∂statement␈α∂to␈α∂execute␈α∂becomes␈α∂the␈α∂one␈α∂with␈α∂the␈α∂corresponding␈α∂label.␈α∂ If␈α∂the␈α∂next
␈↓ ↓H␈↓statement␈α⊂is␈α⊂a␈α⊂␈↓αreturn␈↓␈α⊂then␈α⊂the␈α⊂result␈α⊂is␈α⊂the␈α⊂current␈α⊂value␈α⊂of␈α⊂the␈α⊂term␈α⊂to␈α⊂be␈α⊂returned.␈α⊂ If␈α∂the
␈↓ ↓H␈↓statement␈αis␈α
one␈αof␈α
the␈αconditionals␈αfor␈α
which␈αwe␈α
have␈αcreated␈αa␈α
new␈αfunction␈α
then␈αthe␈α
result␈αis
␈↓ ↓H␈↓that␈αfunction␈αapplied␈αto␈αthe␈αcurrent␈αvalues␈αof␈αthe␈αlocal␈αvariables.␈α If␈αwe␈αare␈αat␈αthe␈αend␈αof␈αthe␈αlist
␈↓ ↓H␈↓of␈αstatements␈αthen␈αthe␈αresult␈αis␈αundefined.␈α Finally␈αthe␈α␈↓αprogram␈↓␈αterm␈αis␈αreplaced␈αby␈αthe␈αresult␈αof
␈↓ ↓H␈↓executing␈α
the␈α∞sequence␈α
of␈α∞statements␈α
starting␈α∞with␈α
the␈α∞first,␈α
with␈α∞the␈α
current␈α∞values␈α
of␈α∞the␈α
local
␈↓ ↓H␈↓variables undefined.
␈↓ ↓H␈↓ [In␈α⊃order␈α⊃to␈α∩insure␈α⊃that␈α⊃this␈α⊃process␈α∩will␈α⊃terminate␈α⊃we␈α⊃must␈α∩keep␈α⊃track␈α⊃of␈α∩the␈α⊃labeled
␈↓ ↓H␈↓statements␈αvisited␈αin␈αan␈αexecution␈αsequence␈αso␈αthat␈αif␈αa␈αlabel␈αgets␈αvisited␈αtwice␈αunconditionally␈αwe
␈↓ ↓H␈↓know that this will be an infinite loop and the result of executing this sequence will be defined.]
␈↓ ↓H␈↓ As␈αan␈αexample,␈αconsider␈αthe␈αfollowing␈αexpression␈αthat␈αgives␈αthe␈αresult␈αof␈αappending␈αthe␈αlist
␈↓ ↓H␈↓␈↓↓a␈↓ with the reversal of ␈↓↓a*b:␈↓
␈↓ ↓H␈↓␈↓ α∞␈↓↓a*␈↓αprogram␈↓↓[[u,v] u←a*b; v←␈↓¬NIL␈↓↓; loop: ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ v; v←␈↓αa|␈↓↓u . v; u←␈↓αd|␈↓↓u; ␈↓αgo to␈↓↓ loop].␈↓
␈↓ ↓H␈↓This expression can be replaced by
␈↓ ↓H␈↓␈↓ ∧H␈↓↓a * loop[a*b, ␈↓¬NIL␈↓↓] ␈↓
␈↓ ↓H␈↓and the recursive definition
␈↓ ↓H␈↓␈↓ ∧∩␈↓↓loop[u,v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ loop[d u,␈↓αa|␈↓↓u . n].␈↓
␈↓ ↓H␈↓ A␈α
second␈α
method␈α
for␈α
transforming␈α∞sequential␈α
terms␈α
into␈α
functional␈α
terms␈α
is␈α∞the␈α
following.
␈↓ ↓H␈↓There␈α∂will␈α∂be␈α∂a␈α∂new␈α∂function␈α∂associated␈α∂with␈α∞each␈α∂label.␈α∂ We␈α∂process␈α∂the␈α∂list␈α∂of␈α∂statements␈α∞in
␈↓ ↓H␈↓reverse␈αorder␈α
starting␈αwith␈α
the␈αlast␈α
one␈αand␈α
carry␈αalong␈α
a␈αcurrent␈α
result.␈α We␈α
begin␈αwith␈αthe␈α
result
␈↓ ↓H␈↓undefined.␈α If␈αthe␈αstatement␈αjust␈αprocessed␈αis␈α
labeled␈αthen␈αwe␈αmake␈αthe␈αdefining␈αexpression␈αof␈α
the
␈↓ ↓H␈↓function␈α
corresponding␈αto␈α
that␈αlabel␈α
equal␈αto␈α
the␈αcurrent␈α
result,␈αand␈α
the␈αcurrent␈α
result␈αbecomes␈α
the
␈↓ ↓H␈↓corresponding␈αfunction␈αapplied␈αto␈αthe␈αlist␈αof␈αlocal␈αvariables.␈α If␈αwe␈αare␈αat␈αthe␈αtop␈αof␈αthe␈αlist␈αthen
␈↓ ↓H␈↓we␈α∂are␈α∞done␈α∂and␈α∞the␈α∂current␈α∞result␈α∂is␈α∞the␈α∂desired␈α∞functional␈α∂term.␈α∞ If␈α∂the␈α∞next␈α∂statement␈α∂is␈α∞an
␈↓ ↓H␈↓assignment␈αwe␈αreplace␈αall␈αfree␈αoccurrences␈αof␈αthe␈αassigned␈αvariable␈αin␈αthe␈αresult␈αby␈αthe␈αexpression
␈↓ ↓H␈↓part␈αof␈αthe␈αassignment.␈α If␈αthe␈αstatement␈αis␈αa␈α␈↓αreturn␈↓␈αwe␈αreplace␈αthe␈αresult␈αby␈αthe␈αexpression␈αto␈αbe
␈↓ ↓H␈↓returned. If the statement is a conditional of the form
␈↓ ↓H␈↓␈↓ ∧1␈↓↓␈↓αif␈↓↓ p1 ␈↓αthen␈↓↓ s1 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ... ␈↓αelse␈↓↓ ␈↓αif␈↓↓ pn ␈↓αthen␈↓↓ sn␈↓
␈↓ ↓H␈↓then the result is replaced by a conditional of the form
␈↓ ↓H␈↓␈↓ ∧¬␈↓↓␈↓αif␈↓↓ p1 ␈↓αthen␈↓↓ t1 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ... ␈↓αelse␈↓↓ ␈↓αif␈↓↓ pn ␈↓αthen␈↓↓ tn ␈↓αelse␈↓↓ t0␈↓
␈↓ ↓H␈↓where␈α
␈↓↓ti␈↓␈α
is␈α
the␈α
result␈αof␈α
processing␈α
the␈α
statements␈α
␈↓↓si␈↓␈α
(bottom␈αup)␈α
starting␈α
with␈α
the␈α
current␈αresult␈α
(␈↓↓t0␈↓
␈↓ ↓H␈↓96␈↓ ¬|Chapter V␈↓ H
␈↓ ↓H␈↓is␈αjust␈αthe␈α
current␈αresult).␈α If␈α
the␈αstatement␈αis␈α
a␈α␈↓αgo to␈↓␈αthen␈α
the␈αresult␈αis␈α
replaced␈αby␈αthe␈α
application
␈↓ ↓H␈↓of the function corresponding to the label to the list of local variables.
␈↓ ↓H␈↓ The␈α∂first␈α∂method␈α∂is␈α∞based␈α∂on␈α∂the␈α∂technique␈α∞for␈α∂transforming␈α∂Flow␈α∂Charts␈α∂or␈α∞ALGOLic
␈↓ ↓H␈↓programs␈α
into␈αrecursive␈α
functional␈αprograms␈α
given␈αin␈α
McCarthy[1962b]␈αThe␈α
second␈αtechnique␈α
for
␈↓ ↓H␈↓converting␈α
sequential␈α
programs␈α∞into␈α
functional␈α
programs␈α∞was␈α
suggested␈α
by␈α∞Richard␈α
Weyhrauch.
␈↓ ↓H␈↓It␈αis␈αbased␈αon␈αthe␈αgeneral␈αmethod␈αused␈αby␈αmathematical␈αlogicians␈αto␈αcode␈αinformal␈αdescriptions␈αof
␈↓ ↓H␈↓sequential␈αcomputations␈αinto␈αfunctional␈αform␈α(typically␈αinto␈αλ-calculus).␈α Both␈αprocedures␈αgive␈αthe
␈↓ ↓H␈↓same␈α∞result␈α
on␈α∞the␈α∞simple␈α
example␈α∞given␈α∞above␈α
as␈α∞well␈α
as␈α∞for␈α∞the␈α
program␈α∞for␈α∞␈↓↓append␈↓␈α
(IV.1.4)
␈↓ ↓H␈↓and␈αa␈α
cleaned␈αup␈α
version␈α(adding␈αan␈α
additional␈αlocal␈α
variable␈αto␈αeliminate␈α
assignments␈αto␈α
a␈αnon
␈↓ ↓H␈↓local variable) of the ␈↓↓reverse␈↓ program (IV.1.1).
␈↓ ↓H␈↓ The␈α⊂above␈α⊃conditions␈α⊂permit␈α⊃a␈α⊂purely␈α⊂local␈α⊃rewrite,␈α⊂ and␈α⊃they␈α⊂are␈α⊃indeed␈α⊂unnecessarily
␈↓ ↓H␈↓restrictive.␈α
The␈αquestion␈α
is␈αthen␈α
how␈α
to␈αrelax␈α
the␈αrestricitions␈α
in␈α
some␈αuseful␈α
way.␈α In␈α
any␈αcase␈α
we
␈↓ ↓H␈↓must␈α
maintain␈α
the␈αproperty␈α
that␈α
replacing␈α
an␈αoccurence␈α
of␈α
the␈α
sequential␈αterm␈α
in␈α
an␈αexpression␈α
by
␈↓ ↓H␈↓its␈αvalue␈α will␈α
not␈αchange␈αthe␈α
value␈αof␈αthe␈αexpression.␈α
It␈αseems␈αevident␈α
that␈αif␈αan␈αouter␈α
sequential
␈↓ ↓H␈↓program␈α∂containing␈α∂an␈α∞inner␈α∂one␈α∂satisfies␈α∞the␈α∂initialization␈α∂and␈α∞local␈α∂assignment␈α∂(i.e.␈α∂ no␈α∞side-
␈↓ ↓H␈↓effect)␈α⊃conditions,␈α⊃then␈α⊃the␈α⊃program␈α⊃can␈α∩be␈α⊃converted␈α⊃to␈α⊃functional␈α⊃form␈α⊃even␈α⊃if␈α∩it␈α⊃contains
␈↓ ↓H␈↓sequential␈α∞subprograms␈α∞that␈α∞violate␈α∞these␈α∞conditions␈α∞provided␈α∞that␈α∞all␈α∞assignments␈α∞in␈α∞the␈α∞inner
␈↓ ↓H␈↓program␈α
are␈α
at␈α
least␈α
local␈α
to␈α
the␈α
outer␈α
program,␈α
and␈α
that␈α
these␈α
inner␈α
programs␈α
can␈α
be␈αreplaced␈α
by
␈↓ ↓H␈↓a␈αfunctional␈αterm␈αin␈αsuch␈αa␈αway␈αthat␈αthe␈αvalue␈αof␈αthe␈αouter␈αsequential␈αterm␈αis␈αnot␈αchanged.␈α The
␈↓ ↓H␈↓rules for doing this have not been formulated as yet.
␈↓ ↓H␈↓ The␈αreason␈αfor␈αbeing␈αparticular␈αabout␈αthe␈αkind␈αof␈αassignments␈αallowed␈αis␈αthat␈αif␈αa␈αprogram
␈↓ ↓H␈↓should␈α
make␈α
assignments␈α
to␈α
external␈α∞variables,␈α
ambiguities␈α
may␈α
arise.␈α
Suppose␈α
we␈α∞evaluate␈α
the
␈↓ ↓H␈↓expression
␈↓ ↓H␈↓␈↓ ∧ ␈↓↓<x, ␈↓αprogram␈↓↓[[] x ← ␈↓αd|␈↓↓x; ␈↓αreturn␈↓↓ x], x>. ␈↓
␈↓ ↓H␈↓when␈α␈↓↓x␈↓␈αhas␈αthe␈αvalue␈α␈↓¬(A B).␈α␈↓␈αIf␈αthe␈αarguments␈αof␈αthe␈αlist␈αare␈α"executed"␈αin␈αleft␈αto␈αright␈αorder,␈αthe
␈↓ ↓H␈↓expression␈αwill␈αhave␈αvalue␈α␈↓¬((A B) (B) (B)),␈α␈↓␈αwhereas␈αif␈αthe␈αexecution␈αis␈αfrom␈αright␈αto␈αleft,␈αthe␈αvalue
␈↓ ↓H␈↓will␈α∞be␈α∞␈↓¬((B) (B) (A B)).␈α∞␈↓␈α
However,␈α∞the␈α∞mathematical␈α∞semantics␈α
of␈α∞expressions␈α∞don't␈α∞prescribe␈α
any
␈↓ ↓H␈↓order␈α
of␈αevaluation,␈α
and␈α
experience␈αwith␈α
ALGOL 60,␈α
which␈αprescribed␈α
left-to-right␈αevaluation␈α
in
␈↓ ↓H␈↓order␈α
to␈α
give␈α
a␈αdefinite␈α
meaning␈α
to␈α
function␈α
procedures␈αthat␈α
alter␈α
external␈α
variables,␈α
has␈αshown
␈↓ ↓H␈↓that the quality of compiled code is greatly reduced by prescribing a fixed order.
␈↓ ↓H␈↓ This␈α
ambiguity␈αcan␈α
be␈α
resolved␈αby␈α
rewriting␈α
expressions␈αcontaining␈α
programs␈α
that␈αchange
␈↓ ↓H␈↓variables␈α
in␈α
the␈α
expression␈α
using␈α
lambda␈αexpressions␈α
to␈α
force␈α
a␈α
desired␈α
order␈α
of␈αevaluation.␈α
Thus
␈↓ ↓H␈↓the above expression could be written
␈↓ ↓H␈↓␈↓ ∧∧␈↓↓[λy:[λz:<z,y,y>][␈↓αprogram␈↓↓[[] x ← ␈↓αd|␈↓↓x; ␈↓αreturn␈↓↓ x]]][x]␈↓
␈↓ ↓H␈↓which␈αwould␈α
evaluate␈αto␈α
␈↓¬((B) (A B) (A B))␈↓␈αin␈αthe␈α
above␈αcase.␈α
Seven␈αother␈α
results␈αcould␈αbe␈α
obtained
␈↓ ↓H␈↓by␈αputting␈α
different␈αcombinations␈α
of␈α␈↓↓z␈↓␈αand␈α
␈↓↓y␈↓␈αbetween␈α
the␈αpointy␈α
brackets.␈α Once␈αthese␈α
ambiguities
␈↓ ↓H␈↓have␈αbeen␈αresolved,␈αit␈αwould␈αseem␈αthat␈αwe␈αcould␈αtranslate␈αan␈αouter␈αsequential␈αprogram␈α
containing
␈↓ ↓H␈↓inner␈α⊂sequential␈α⊂programs␈α⊂into␈α⊂a␈α⊂function␈α⊂program␈α⊂in␈α⊂a␈α⊂unique␈α⊂way␈α⊂even␈α⊂allowing␈α⊂the␈α∂inner
␈↓ ↓H␈↓programs␈α⊃to␈α⊂make␈α⊃assignments␈α⊂to␈α⊃variables␈α⊃in␈α⊂the␈α⊃outer␈α⊂programs.␈α⊃ The␈α⊂rules␈α⊃for␈α⊃doing␈α⊂this
␈↓ ↓H␈↓haven't been worked out, however.
␈↓ ↓H␈↓␈↓ ¬|Chapter V␈↓ *97
␈↓ ↓H␈↓α␈↓ ε
Exercises
␈↓ ↓H␈↓1.␈α
Implement␈α
one␈α
of␈αthe␈α
algorithms␈α
described␈α
above␈α
for␈αtransforming␈α
a␈α
sequential␈α
LISP␈αterm␈α
into
␈↓ ↓H␈↓a functional LISP term.
␈↓ ↓H␈↓2. ␈↓αDerived programs ␈↓
␈↓ ↓H␈↓ So␈α∪far␈α∀we␈α∪have␈α∪discussed␈α∀mainly␈α∪extensional␈α∪properties␈α∀of␈α∪programs.␈α∪ Recall␈α∀that␈α∪an
␈↓ ↓H␈↓extensional␈α
property␈α
is␈α
a␈α
property␈α
that␈α
depends␈α
only␈α
on␈α
the␈α
function␈α
computed␈α
by␈α∞the␈α
program.
␈↓ ↓H␈↓In␈αmany␈αcases␈αwe␈αwould␈αlike␈αto␈α
be␈αable␈αto␈αexpress␈αand␈αprove␈αthings␈αabout␈α
intensional␈αproperties
␈↓ ↓H␈↓such␈α_as␈α_space␈α_or␈α_time␈α_requirements,␈α_depth␈α_of␈α_recursion,␈α_etc..␈α_ Thus␈α_the␈α→statement␈α_that
␈↓ ↓H␈↓␈↓↓∀u.flatten x=fringe x␈↓␈α⊂ expresses␈α⊃the␈α⊂extensional␈α⊃property␈α⊂that␈α⊂␈↓↓flatten␈↓␈α⊃(I.8.8)␈α⊂and␈α⊃␈↓↓fringe␈↓␈α⊂(I.8.10)
␈↓ ↓H␈↓compute␈α
the␈αsame␈α
function.␈α However␈α
the␈α
statement␈αmade␈α
in␈αChapter␈α
I␈αthat␈α
␈↓↓flatten␈↓␈α
requires␈αless
␈↓ ↓H␈↓computation␈α∞than␈α∞␈↓↓fringe␈↓␈α∞refers␈α
to␈α∞an␈α∞intensional␈α∞property.␈α
We␈α∞could␈α∞make␈α∞this␈α∞statement␈α
more
␈↓ ↓H␈↓precise␈α
by␈αsaying␈α
that␈α
more␈α␈↓↓cons␈↓es␈α
are␈α
executed␈αin␈α
computing␈α
␈↓↓fringe x␈↓␈αthan␈α
in␈αcomputing␈α
␈↓↓flatten x␈↓
␈↓ ↓H␈↓but the methods of Chapter III do not allow us to prove it.
␈↓ ↓H␈↓ Consider␈α∀the␈α∀problem␈α∀of␈α∀computing␈α∀the␈α∀number␈α∀of␈α∀␈↓↓cons␈↓es␈α∀executed␈α∀when␈α∀computing
␈↓ ↓H␈↓␈↓↓flatten x.␈↓␈α
Since␈α
␈↓↓flatten␈↓␈α
simply␈α
calls␈α
the␈α
auxiliary␈αfunction␈α
␈↓↓flat␈↓␈α
we␈α
will␈α
do␈α
the␈α
problem␈α
for␈α␈↓↓flat.␈↓
␈↓ ↓H␈↓There␈α⊂are␈α⊂two␈α⊃cases.␈α⊂ If␈α⊂␈↓↓x␈↓␈α⊃is␈α⊂an␈α⊂atom␈α⊃then␈α⊂␈↓↓flat[x,u]=x . u␈↓␈α⊂so␈α⊃1␈α⊂␈↓↓cons␈↓␈α⊂is␈α⊃executed.␈α⊂ Otherwise
␈↓ ↓H␈↓␈↓↓flat[x,u]=flat[␈↓αa|␈↓↓x,flat[␈↓αd|␈↓↓x,u]␈↓␈αso␈αthe␈αnumber␈αof␈α␈↓↓cons␈↓es␈αexecuted␈αmust␈αbe␈αthe␈αsum␈αof␈αthose␈α
executed
␈↓ ↓H␈↓in␈α∂computing␈α∂␈↓↓flat[␈↓αd|␈↓↓x,u]␈↓␈α∂and␈α⊂those␈α∂executed␈α∂␈↓↓flat[␈↓αa|␈↓↓x,v]␈↓␈α∂where␈α∂␈↓↓v␈↓␈α⊂is␈α∂␈↓↓flat[␈↓αd|␈↓↓x,u]␈↓.␈α∂ The␈α∂idea␈α⊂is␈α∂to
␈↓ ↓H␈↓count␈αthe␈α␈↓↓cons␈↓es␈αexecuted␈αin␈αcomputing␈αthe␈αvalues␈αof␈αthe␈αarguments␈αand␈αadd␈αto␈αthat␈αthe␈αnumber
␈↓ ↓H␈↓of␈α␈↓↓cons␈↓␈αexecuted␈αin␈αcomputing␈αthe␈αprogram␈αwith␈αthose␈αvalues␈αas␈αarguments.␈α Thus␈αwe␈αcan␈αdefine
␈↓ ↓H␈↓the function ␈↓↓cflat␈↓ that computes the number of ␈↓↓cons␈↓es executed in computing ␈↓↓flat␈↓ by
␈↓ ↓H␈↓↓␈↓ βxcflat[x, u] ←
␈↓ ↓H␈↓↓2.1)␈↓ ∧8␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ ␈↓¬1␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ cflat[␈↓αa|␈↓↓x, flat[␈↓αd|␈↓↓x, u]] + cflat[␈↓αd|␈↓↓x, u]
␈↓ ↓H␈↓for example
␈↓ ↓H␈↓␈↓ ¬β␈↓↓cflat[␈↓¬(A . (B . (C . D)))␈↓↓, ␈↓¬NIL␈↓↓] = ␈↓¬4␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬β␈↓↓cflat[␈↓¬(((A . B) . C) . D)␈↓↓, ␈↓¬NIL␈↓↓] = ␈↓¬4␈↓↓␈↓
␈↓ ↓H␈↓We␈αcan␈α
express␈αthe␈α
function␈αcomputed␈α
by␈α␈↓↓cflat␈↓␈α
in␈αterms␈α
of␈αthe␈α
size␈αof␈α
the␈αinput␈α
using␈α␈↓↓size␈↓␈α
(III.9.7)
␈↓ ↓H␈↓by
␈↓ ↓H␈↓2.2)␈↓ ¬&␈↓↓∀x u: cflat[x, u] = size x␈↓.
␈↓ ↓H␈↓To␈α∃prove␈α⊗this␈α∃statement␈α⊗we␈α∃us␈α⊗S-expresion␈α∃induction␈α∃on␈α⊗␈↓↓x.␈↓␈α∃ In␈α⊗the␈α∃case␈α⊗␈↓↓x␈↓␈α∃is␈α⊗an␈α∃atom
␈↓ ↓H␈↓␈↓↓∀u.cflat[x,u]=␈↓¬1␈↓↓=sixe x␈↓␈α'by␈α&simplifying␈α'the␈α'definitions.␈α& Otherwise␈α'we␈α'may␈α&assume
␈↓ ↓H␈↓␈↓↓∀u.cflat[␈↓αa|␈↓↓x,u]=size ␈↓αa|␈↓↓x␈↓ and ␈↓↓∀u.cflat[␈↓αd|␈↓↓x,u]=size ␈↓αd|␈↓↓x␈↓ . Now
␈↓ ↓H␈↓␈↓ αx␈↓↓cflat[x,u]␈↓ βx=cflat[␈↓αa|␈↓↓x,flat[␈↓αd|␈↓↓x,u]]+cflat[␈↓αd|␈↓↓x,u]␈↓␈↓ λ8definition of ␈↓↓flat␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓=size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x␈↓␈↓ λ8induction hypothesis
␈↓ ↓H␈↓␈↓ βx␈↓↓=size x␈↓␈↓ λ8definition of ␈↓↓size␈↓
␈↓ ↓H␈↓98␈↓ ¬|Chapter V␈↓ H
␈↓ ↓H␈↓Notice that in order to make the second step we need to know that ␈↓↓flat␈↓ is total.
␈↓ ↓H␈↓ Now␈αwe␈αwill␈α
do␈αthe␈αsame␈αconstruction␈α
for␈α␈↓↓fringe.␈↓␈α When␈α␈↓↓x␈↓␈α
is␈αan␈αatom␈αthen␈α
␈↓↓fringe x=x . ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓so␈α
the␈αnumber␈α
of␈α
␈↓↓cons␈↓es␈αis␈α
␈↓¬1.␈α
␈↓␈αOtherwise␈α
␈↓↓fringe x=fringe ␈↓αa|␈↓↓x * fringe ␈↓αd|␈↓↓x␈↓.␈α
So␈αwe␈α
count␈αthe␈α
number
␈↓ ↓H␈↓of␈α
␈↓↓cons␈↓es␈αexecuted␈α
in␈αcomputing␈α
␈↓↓fringe ␈↓αa|␈↓↓x␈↓␈α
and␈α ␈↓↓fringe ␈↓αd|␈↓↓x␈↓␈α
and␈αadd␈α
to␈α
this␈αthe␈α
number␈αexecuted␈α
in
␈↓ ↓H␈↓computing␈α∞␈↓↓u * v␈↓␈α∞where␈α∞␈↓↓u␈↓␈α∞is␈α∞␈↓↓fringe ␈↓αa|␈↓↓x␈↓␈α∞and␈α∂␈↓↓v␈↓␈α∞is␈α∞␈↓↓fringe ␈↓αd|␈↓↓x␈↓.␈α∞ A␈α∞similar␈α∞analysis␈α∞can␈α∞be␈α∂made␈α∞for
␈↓ ↓H␈↓␈↓↓append.␈↓ Thus we can define the functions ␈↓↓cfringe␈↓ and ␈↓↓cappend␈↓ by
␈↓ ↓H␈↓↓␈↓ βxcfringe x ←
␈↓ ↓H␈↓↓2.3)␈↓ ∧8␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ ␈↓¬1␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ cappend[fringe ␈↓αa|␈↓↓x, fringe ␈↓αd|␈↓↓x] + cfringe ␈↓αa|␈↓↓x + cfringe ␈↓αd|␈↓↓x
␈↓ ↓H␈↓↓␈↓and␈↓↓
␈↓ ↓H␈↓↓␈↓ βxcappend[u,v] ←
␈↓ ↓H␈↓↓2.4)␈↓ ∧8␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ ␈↓¬1 ␈↓↓+ cappend[␈↓αd|␈↓↓u, v]
␈↓ ↓H␈↓For example
␈↓ ↓H␈↓␈↓ ¬∪␈↓↓cfringe[␈↓¬(A . (B . (C . D)))␈↓↓] = ␈↓¬7␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬␈↓↓cfringe[␈↓¬(((A . B) . C) . D)␈↓↓] = ␈↓¬10␈↓↓␈↓
␈↓ ↓H␈↓We can express ␈↓↓cfringe␈↓ in terms of the size of the input by
␈↓ ↓H␈↓␈↓ ¬⊂␈↓↓∀x: cfringe x ≥ ␈↓¬2 ␈↓↓␈↓π*␈↓↓ size x - ␈↓¬1␈↓↓␈↓
␈↓ ↓H␈↓2.5)
␈↓ ↓H␈↓␈↓ ∧T␈↓↓∀x: [size x + ␈↓¬1␈↓↓]␈↓π*␈↓↓[size x] ≥ ␈↓¬2 ␈↓↓␈↓π*␈↓↓ cfringe x␈↓
␈↓ ↓H␈↓This can be proved by S-expression using the fact that
␈↓ ↓H␈↓2.6)␈↓ ∧r␈↓↓∀x u.cappend[fringe x,u]=size x␈↓.
␈↓ ↓H␈↓In␈α⊂the␈α⊂case␈α∂␈↓↓x␈↓␈α⊂is␈α⊂atomic␈α∂the␈α⊂inequalities␈α⊂reduce␈α∂to␈α⊂1≥1␈α⊂and␈α∂2≥2␈α⊂using␈α⊂the␈α⊂function␈α∂definitions.
␈↓ ↓H␈↓Otherwise we have
␈↓ ↓H␈↓␈↓ αx␈↓↓cfringe x␈↓ βx= cappend[fringe ␈↓αa|␈↓↓x,fringe ␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ + cfringe ␈↓αa|␈↓↓x + cfringe ␈↓αd|␈↓↓x ␈↓␈↓ λ8by definition of ␈↓↓cfringe␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓≥ cappend[fringe ␈↓αa|␈↓↓x,fringe ␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ + ␈↓¬2␈↓↓␈↓π*␈↓↓[size ␈↓αa|␈↓↓x-␈↓¬1␈↓↓] + ␈↓¬2␈↓↓␈↓π*␈↓↓[size ␈↓αd|␈↓↓x-␈↓¬1␈↓↓]␈↓␈↓ λ8by induction
␈↓ ↓H␈↓␈↓ βx␈↓↓≥ size ␈↓αa|␈↓↓x + ␈↓¬2␈↓↓␈↓π*␈↓↓[size x-␈↓¬2␈↓↓]␈↓␈↓ λ8by definition of ␈↓↓size␈↓ and 2.6
␈↓ ↓H␈↓␈↓ βx␈↓↓≥ ␈↓¬2␈↓↓␈↓π*␈↓↓[size x-␈↓¬1␈↓↓]␈↓␈↓ λ8since ␈↓↓size ␈↓αa|␈↓↓x␈↓≥␈↓¬1␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ αX␈↓↓␈↓¬2␈↓↓␈↓π*␈↓↓cfringe x␈↓ βx= ␈↓¬2␈↓↓␈↓π*␈↓↓[cappend[fringe ␈↓αa|␈↓↓x,fringe ␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ + cfringe ␈↓αa|␈↓↓x + cfringe ␈↓αd|␈↓↓x]␈↓␈↓ λ_by definition of ␈↓↓cfringe␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓≤ ␈↓¬2␈↓↓␈↓π*␈↓↓size ␈↓αa|␈↓↓x ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ + [size ␈↓αa|␈↓↓x+␈↓¬1␈↓↓]␈↓π*␈↓↓[size ␈↓αa|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ + [size ␈↓αd|␈↓↓x+␈↓¬1␈↓↓]␈↓π*␈↓↓[size ␈↓αd|␈↓↓x]␈↓␈↓ λ_by induction and 2.6
␈↓ ↓H␈↓␈↓ βx␈↓↓≤ [sixe ␈↓αa|␈↓↓x+size ␈↓αd|␈↓↓x+␈↓¬1␈↓↓]␈↓π*␈↓↓[size qa+size ␈↓αd|␈↓↓x]␈↓␈↓ λ_by computation
␈↓ ↓H␈↓␈↓ βx␈↓↓≤ [sixe x+␈↓¬1␈↓↓]␈↓π*␈↓↓[size x]␈↓␈↓ λ_by definition of ␈↓↓size␈↓
␈↓ ↓H␈↓␈↓ ¬|Chapter V␈↓ *99
␈↓ ↓H␈↓Thus␈αwith␈αthe␈αaid␈αof␈αthe␈αderived␈αprograms␈α␈↓↓cflat␈↓␈αand␈α␈↓↓cfringe␈↓␈αwe␈αare␈αable␈αto␈αmake␈αa␈αquantitative
␈↓ ↓H␈↓comparison of the amount of computation done by ␈↓↓flat␈↓ and ␈↓↓fringe.␈↓
␈↓ ↓H␈↓ The␈α
derived␈α
program␈α
that␈α
computes␈α
the␈α
number␈α
of␈α
␈↓↓cons␈↓es␈α
executed␈α
in␈α
computing␈α
a␈α
program
␈↓ ↓H␈↓is␈α∞an␈α∞example␈α∞of␈α∂a␈α∞general␈α∞class␈α∞of␈α∞␈↓↓cost␈↓␈α∂computing␈α∞derived␈α∞programs.␈α∞ The␈α∞rules␈α∂for␈α∞obtaining
␈↓ ↓H␈↓such␈αa␈α
derived␈αprogram␈αfrom␈α
the␈αoriginal␈α
program␈αdefinition␈αare␈α
as␈αfollows.␈α
First␈αassociate␈αa␈α
cost
␈↓ ↓H␈↓program␈α
with␈α∞each␈α
of␈α
the␈α∞function␈α
applications␈α
occuring␈α∞in␈α
defining␈α
expression.␈α∞ (This␈α
includes
␈↓ ↓H␈↓associating␈α
the␈αprogram␈α
to␈αbe␈α
derived␈αwith␈α
the␈αoriginal␈α
program.)␈α Then␈α
transform␈α
the␈αdefining
␈↓ ↓H␈↓expression.␈α If␈αthe␈αexpression␈αis␈αa␈α
constant␈αor␈αvariable␈αthen␈αthe␈αcost␈α
is␈α␈↓¬0.␈α␈↓␈αIf␈αthe␈αexpression␈α
is␈αa
␈↓ ↓H␈↓conditional of the form
␈↓ ↓H␈↓␈↓ ¬N␈↓↓␈↓αif␈↓↓ p ␈↓αthen␈↓↓ t␈↓β1␈↓↓ ␈↓αelse␈↓↓ t␈↓β2␈↓↓␈↓
␈↓ ↓H␈↓then it is transformed into
␈↓ ↓H␈↓␈↓ ∧+␈↓↓<cost(p)>+␈↓αif␈↓↓ p ␈↓αthen␈↓↓ <cost(t␈↓β1␈↓↓)> ␈↓αelse␈↓↓ <cost(t␈↓β2␈↓↓)>␈↓.
␈↓ ↓H␈↓where␈α␈↓↓<cost(e)>␈↓␈α
is␈αthe␈α
cost␈αterm␈α
derived␈αby␈α
applying␈αthe␈α
rules␈αto␈α
to␈α␈↓↓e.␈↓␈α
If␈αthe␈α
expression␈αis␈α
a␈αlist
␈↓ ↓H␈↓forming␈α⊂operation␈α⊂we␈α⊂replace␈α∂it␈α⊂by␈α⊂the␈α⊂equivalent␈α⊂composition␈α∂of␈α⊂␈↓↓cons␈↓es␈α⊂and␈α⊂then␈α⊂proceed␈α∂as
␈↓ ↓H␈↓usual.␈α⊂ If␈α⊂the␈α∂expression␈α⊂is␈α⊂the␈α⊂application␈α∂of␈α⊂a␈α⊂function,␈α∂␈↓↓f,␈↓␈α⊂to␈α⊂a␈α⊂list␈α∂of␈α⊂arguments␈α⊂then␈α⊂it␈α∂is
␈↓ ↓H␈↓transformed␈αinto␈αthe␈αsum␈αof␈αthe␈αcosts␈α
of␈αthe␈αarguments␈αplus␈αthe␈αcost␈αfunction␈αcorresponding␈α
to␈α␈↓↓f␈↓
␈↓ ↓H␈↓applied␈α∂to␈α∂the␈α∂argument␈α∂values.␈α∂ If␈α∂the␈α∂expression␈α∂is␈α∂a␈α∂lambda␈α∂expression␈α∂applied␈α∂to␈α∂a␈α∂list␈α∂of
␈↓ ↓H␈↓arguments␈α
it␈α
is␈α
transformed␈α
into␈α
the␈αsum␈α
of␈α
the␈α
costs␈α
of␈α
the␈αarguments␈α
plus␈α
the␈α
cost␈α
of␈α
the␈αbody␈α
of
␈↓ ↓H␈↓the lambda expression.
␈↓ ↓H␈↓ Thus␈α⊂if␈α⊂we␈α∂put␈α⊂␈↓↓ccons[x,y]←␈↓¬1␈↓↓␈↓␈α⊂and␈α∂all␈α⊂other␈α⊂cost␈α∂functions␈α⊂except␈α⊂␈↓↓cflat␈↓␈α∂equal␈α⊂to␈α⊂the␈α∂zero
␈↓ ↓H␈↓function␈αand␈αapply␈αthe␈αabove␈αalgorithm␈αwe␈αobtain␈αthe␈αdefinition␈αof␈α␈↓↓cflat␈↓␈αgiven␈αabove.␈α Similarly
␈↓ ↓H␈↓for ␈↓↓cfringe.␈↓
␈↓ ↓H␈↓ There␈α⊂are␈α∂other␈α⊂intensional␈α⊂properties␈α∂of␈α⊂interest.␈α∂For␈α⊂example␈α⊂the␈α∂number␈α⊂of␈α⊂calls␈α∂␈↓↓flat␈↓
␈↓ ↓H␈↓makes to itself is given by
␈↓ ↓H␈↓↓␈↓ βxrflat[x, u] ←
␈↓ ↓H␈↓↓2.7)␈↓ ∧8␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ ␈↓¬1 ␈↓↓+ rflat[␈↓αa|␈↓↓x, flat[␈↓αd|␈↓↓x, u]] + rflat[␈↓αd|␈↓↓x, u].
␈↓ ↓H␈↓Thus
␈↓ ↓H␈↓␈↓ ∧⎇␈↓↓rflat[␈↓¬(A . (B . (C . D)))␈↓↓, ␈↓¬NIL␈↓↓] = ␈↓¬7 ␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧z␈↓↓rflat[␈↓¬(((A . B) . C) . D)␈↓↓, ␈↓¬NIL␈↓↓] = ␈↓¬7 ␈↓↓␈↓.
␈↓ ↓H␈↓We can express ␈↓↓rflat␈↓ in terms of the size of the input by
␈↓ ↓H␈↓2.8)␈↓ ∧⎇␈↓↓∀x u: rflat[x, u] = ␈↓¬2␈↓↓␈↓π*␈↓↓size x - ␈↓¬1␈↓↓␈↓.
␈↓ ↓H␈↓The maximum depth of recursion occuring in a computation of ␈↓↓flat␈↓ is given by
␈↓ ↓H␈↓↓␈↓ βxdflat[x, u] ←
␈↓ ↓H␈↓↓2.9)␈↓ ∧8if ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ ␈↓¬0␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ ␈↓¬1 ␈↓↓+ max[dflat[␈↓αa|␈↓↓x, flat[␈↓αd|␈↓↓x, u]], dflat[␈↓αd|␈↓↓x, u]],
␈↓ ↓H␈↓100␈↓ ¬|Chapter V␈↓ H
␈↓ ↓H␈↓so that
␈↓ ↓H␈↓␈↓ ∧{␈↓↓dflat[␈↓¬(A . (B . (C . D)))␈↓↓, ␈↓¬NIL␈↓↓] = ␈↓¬3 ␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧{␈↓↓dflat[␈↓¬((A . B) . (C . D))␈↓↓, ␈↓¬NIL␈↓↓] = ␈↓¬2 ␈↓↓␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓2.10)␈↓ β⎇␈↓↓∀x u: dflat[x, u] = ␈↓¬length of longest ␈↓αa|␈↓¬- ␈↓αd|␈↓¬chain in␈↓↓ x␈↓.
␈↓ ↓H␈↓ A␈α∞somewhat␈α∞more␈α∞complex␈α∞property␈α∞is␈α∞the␈α∂trace␈α∞of␈α∞the␈α∞computation.␈α∞ A␈α∞trace␈α∞is␈α∞a␈α∂list␈α∞of
␈↓ ↓H␈↓n+1-tuples␈α(<arg1>...<argn>␈α<result>)␈αcontaining␈αone␈αsuch␈α
tuple␈αfor␈αeach␈αcall␈αto␈αthe␈αfunction␈α
being
␈↓ ↓H␈↓traced. Thus if we wish to compute a trace of ␈↓↓flat␈↓ we can use ␈↓↓tflat.␈↓
␈↓ ↓H␈↓↓␈↓ βxtflat[x, u] ←
␈↓ ↓H␈↓↓2.11)␈↓ ∧8␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <<x, u, flat[x, u]>>
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ <x, u, flat[x, u]> . [tflat[␈↓αd|␈↓↓x, u] * tflat[␈↓αa|␈↓↓x, flat[␈↓αd|␈↓↓x, u]]]
␈↓ ↓H␈↓For example the traces of ␈↓↓flat[␈↓¬(A B C . D),NIL␈↓↓]␈↓ and ␈↓↓flat[␈↓¬(((A . B) . C) . D),NIL␈↓↓]␈↓ are
␈↓ ↓H␈↓¬␈↓ βx␈↓↓tflat[(A . (B . (C . D))), ␈↓¬NIL␈↓↓] = ␈↓¬
␈↓ ↓H␈↓¬␈↓ ¬((((A B C . D) NIL (A B C D))
␈↓ ↓H␈↓¬␈↓ ¬8((B C . D) NIL (B C D))
␈↓ ↓H␈↓¬␈↓ ¬8((C . D) NIL (C D))
␈↓ ↓H␈↓¬␈↓ ¬8(D NIL (D))
␈↓ ↓H␈↓¬␈↓ ¬8(C (D) (C D))
␈↓ ↓H␈↓¬␈↓ ¬8(B (C D) (B C D))
␈↓ ↓H␈↓¬␈↓ ¬8(A (B C D) (A B C D)))
␈↓ ↓H␈↓¬␈↓ βx␈↓↓tflat[(((A . B) . C) . D), ␈↓¬NIL␈↓↓] = ␈↓¬
␈↓ ↓H␈↓¬␈↓ ¬((((((A . B) . C) . D) NIL (A B C D))
␈↓ ↓H␈↓¬␈↓ ¬8(D NIL (D))
␈↓ ↓H␈↓¬␈↓ ¬8(((A . B) . C) (D) (A B C D))
␈↓ ↓H␈↓¬␈↓ ¬8(C (D) (C D))
␈↓ ↓H␈↓¬␈↓ ¬8((A . B) (C D) (A B C D))
␈↓ ↓H␈↓¬␈↓ ¬8(B (C D) (B C D))
␈↓ ↓H␈↓¬␈↓ ¬8(A (B C D) (A B C D))))
␈↓ ↓H␈↓We␈αnote␈αthat␈αthis␈αfairly␈αsimple␈αtracing␈αfunction␈αis␈αprobably␈αnot␈αthe␈αmost␈αefficient␈αway␈αto␈αproduce
␈↓ ↓H␈↓the trace of a computation, however it is very easy to understand and work with.
␈↓ ↓H␈↓The␈αidea␈αfor␈αgeneral␈αcost␈αcomputing␈αprograms␈αis␈αdue␈αto␈αBen␈αMoszkoswki.␈α This␈αis␈αdescribed␈αand
␈↓ ↓H␈↓many sample applications are given in Moszkowski[1978].
␈↓ ↓H␈↓α␈↓ ε
Exercises
␈↓ ↓H␈↓1.␈αDefine␈αfunctions␈αto␈αcompute␈αthe␈αnumber␈αof␈α␈↓↓cons␈↓es␈αexecuted␈αby␈αthe␈αprograms␈α␈↓↓reverse␈↓␈α(I.8.7)␈αand
␈↓ ↓H␈↓␈↓↓reversea␈↓ (I.8.9) and work out the relation between these costs.
␈↓ ↓H␈↓␈↓ ¬|Chapter V␈↓ ≠101
␈↓ ↓H␈↓3. ␈↓αSubstantial exercises␈↓
␈↓ ↓H␈↓1.␈αWrite␈αa␈α
program␈αto␈αconvert␈α
functional␈αLISP␈αprograms␈α
into␈αiterative␈α(sequential)␈α
programs␈αfor
␈↓ ↓H␈↓some class of programs.
␈↓ ↓H␈↓2. Implement the algorithm for producing cost computing derived functions described in ␈↓π∞␈↓2.
␈↓ ↓H␈↓3.␈α
Write␈α
a␈α
program␈α
that␈α∞transforms␈α
a␈α
program␈α
into␈α
one␈α∞that␈α
computes␈α
the␈α
trace␈α
of␈α∞a␈α
functional
␈↓ ↓H␈↓LISP program.
␈↓ ↓H␈↓102␈↓ εH␈↓ H
␈↓ ↓H␈↓α␈↓ ¬{Chapter VI
␈↓ ↓H␈↓α␈↓ ¬.ABSTRACT SYNTAX
␈↓ ↓H␈↓1. ␈↓αWhat is Abstract Syntax?␈↓
␈↓ ↓H␈↓ When␈α
we␈α
compute␈α
with␈α
symbolic␈α
expressions,␈α
whether␈α
they␈α
be␈α
computer␈αprograms,␈α
algebraic
␈↓ ↓H␈↓expressions,␈α
or␈α
formulas␈αof␈α
logic,␈α
many␈αaspects␈α
of␈α
their␈αrepresentation␈α
are␈α
irrelevant␈αfor␈α
theoretical
␈↓ ↓H␈↓purposes.␈α
For␈αexample,␈α
the␈αsum␈α
of␈α␈↓↓a␈↓␈α
and␈α␈↓↓b␈↓␈α
may␈α
be␈αwritten␈α
␈↓↓a + b␈↓␈αas␈α
in␈αthe␈α
most␈αcommon␈α
algebraic
␈↓ ↓H␈↓notation,␈α∞␈↓¬(PLUS␈α∞␈↓↓a␈↓¬␈α∞␈↓↓b␈↓¬)␈↓␈α∞as␈α∞in␈α∞LISP,␈α∞␈↓↓+ab␈↓␈α∂in␈α∞"Polish␈α∞notation"␈α∞or␈α∞␈↓↓ab+␈↓␈α∞in␈α∞the␈α∞reverse␈α∂Polish␈α∞notation
␈↓ ↓H␈↓made␈α↔popular␈α↔by␈α↔the␈α↔Hewlett-Packard␈α↔calculators.␈α↔ Mathematical␈α↔logicians␈α↔even␈α⊗represent
␈↓ ↓H␈↓expressions␈αby␈α"G␈↓π:␈↓odel␈αnumbers"␈αwhen␈αthey␈αwant␈αto␈αrepresent␈αsymbolic␈αcomputations␈αas␈αnumerical
␈↓ ↓H␈↓computations.␈α⊂ One␈α∂such␈α⊂G␈↓π:␈↓odel␈α∂numbering␈α⊂would␈α∂represent␈α⊂the␈α∂sum␈α⊂of␈α∂␈↓↓a␈↓␈α⊂and␈α∂␈↓↓b␈↓␈α⊂as␈α⊂2␈↓∧a␈↓3␈↓∧b␈↓.␈α∂ Of
␈↓ ↓H␈↓course,␈α
"the␈αsum␈α
of␈α␈↓↓a␈↓␈α
and␈α
␈↓↓b␈↓"␈αis␈α
just␈αanother␈α
such␈αnotation␈α
which␈α
we␈αhave␈α
been␈αusing␈α
to␈αtalk␈α
about
␈↓ ↓H␈↓the others.
␈↓ ↓H␈↓ If,␈α
for␈α
example,␈α
we␈α
are␈αinterested␈α
in␈α
evaluating␈α
sums␈α
or␈αin␈α
compiling␈α
them,␈α
the␈α
details␈αof␈α
the
␈↓ ↓H␈↓representation␈αmay␈α
be␈αirrelevant.␈α We␈α
need␈αto␈α
be␈αable␈αto␈α
tell␈αwhether␈α
an␈αexpression␈αis␈α
a␈αsum,␈αif␈α
so
␈↓ ↓H␈↓what␈αthe␈αsummands␈αare,␈αand␈αwe␈αneed␈αto␈α
be␈αable␈αto␈αmake␈αsums␈αfrom␈αconstituent␈αexpressions.␈α
The
␈↓ ↓H␈↓predicate␈αthat␈αtells␈α
whether␈αan␈αexpression␈αis␈α
a␈αsum␈αand␈αthe␈α
functions␈αthat␈αextract␈α
the␈αsummands
␈↓ ↓H␈↓comprise␈α⊂the␈α⊂␈↓↓analytic␈α⊂syntax␈↓␈α⊂of␈α⊂sums.␈α⊂ The␈α⊂function␈α⊂that␈α⊂makes␈α⊂a␈α⊂sum␈α⊂expression␈α⊂out␈α⊃of␈α⊂the
␈↓ ↓H␈↓summand␈αexpression␈αgives␈αthe␈α␈↓↓synthetic␈αsyntax␈↓␈αof␈αsums.␈α The␈αanalytic␈αand␈αsynthetic␈αsyntaxes␈αare,
␈↓ ↓H␈↓of course, related to each other.
␈↓ ↓H␈↓ ␈↓↓Abstract␈α∩syntax␈↓,␈α∪first␈α∩described␈α∩in␈α∪[McCarthy␈α∩1962b],␈α∪is␈α∩a␈α∩way␈α∪of␈α∩presenting␈α∪only␈α∩the
␈↓ ↓H␈↓information␈α∪about␈α∩a␈α∪class␈α∪of␈α∩expressions␈α∪that␈α∩is␈α∪relevant␈α∪to␈α∩computing␈α∪with␈α∪them,␈α∩omitting
␈↓ ↓H␈↓irrelevant␈α∩detail.␈α⊃ Later␈α∩we␈α⊃shall␈α∩prove␈α∩a␈α⊃small␈α∩compiler␈α⊃correct␈α∩without␈α⊃ever␈α∩deciding␈α∩on␈α⊃a
␈↓ ↓H␈↓definite␈α∂notation.␈α∂ If␈α∂we␈α∂decide␈α∂on␈α∂a␈α∞notation␈α∂for␈α∂some␈α∂expressions␈α∂and␈α∂write␈α∂programs␈α∂to␈α∞test
␈↓ ↓H␈↓whether␈α∀an␈α∀expression␈α∀is␈α∀of␈α∀a␈α∀given␈α∪type,␈α∀to␈α∀extract␈α∀subexpressions␈α∀and␈α∀to␈α∀construct␈α∪new
␈↓ ↓H␈↓expressions␈α~from␈α→old␈α~ones␈α→then␈α~we␈α~have␈α→give␈α~a␈α→␈↓↓concrete syntax␈↓.␈α~ Any␈α~concrete␈α→syntax
␈↓ ↓H␈↓corresponding to our abstract syntax will do.
␈↓ ↓H␈↓αExample 1. Simplification of arithmetic expressions.
␈↓ ↓H␈↓ Consider␈α
arithmetic␈αexpressions␈α
built␈α
up␈αfrom␈α
constants␈α
and␈αvariables␈α
by␈α
forming␈αsums␈α
and
␈↓ ↓H␈↓products. The abstract syntax is given in table 1.
␈↓ ↓H␈↓↓␈↓α␈↓ αxpredicate␈↓ ∧xanalytic operation␈↓ πxsynthetic operation␈↓↓
␈↓ ↓H␈↓↓␈↓ αxisconst(e)
␈↓ ↓H␈↓↓␈↓ αxisvar(e)
␈↓ ↓H␈↓↓␈↓ αxissum(e)␈↓ ¬_s1(e), s2(e)␈↓ λ8mksum(e1,e2)
␈↓ ↓H␈↓↓␈↓ αxisprod(e)␈↓ ¬_p1(e), p2(e)␈↓ λ8mkprod(e1,e2)
␈↓ ↓H␈↓␈↓ ε∪␈↓αTable 1.␈↓
␈↓ ↓H␈↓␈↓ ¬wChapter VI␈↓ ≠103
␈↓ ↓H␈↓ The␈α␈↓αpredicate␈↓␈αcolumn␈αasserts␈αthat␈αthe␈αexpressions␈αcomprise␈αconstants,␈αvariables␈αand␈αbinary
␈↓ ↓H␈↓sums␈α
and␈α
procucts,␈α
and␈α
that␈α
the␈α
predicates␈α
␈↓↓isconst,␈↓␈α
␈↓↓isvar,␈↓␈α
␈↓↓issum,␈↓␈α
and␈α
␈↓↓isprod␈↓␈α
enable␈α
one␈α
to␈α
classify
␈↓ ↓H␈↓each␈α∪expression.␈α∪ The␈α∪␈↓αanalytic␈α∪operations␈↓␈α∪column␈α∩tells␈α∪us␈α∪that␈α∪variables␈α∪and␈α∪constants␈α∩are
␈↓ ↓H␈↓"atomic"␈αexpressions,␈αwhile␈αsums␈αand␈α
products␈αhave␈αsubexpressions␈αobtained␈αby␈αapplying␈α
␈↓↓s1␈↓␈αand
␈↓ ↓H␈↓␈↓↓s2␈↓ in the case of sums, or ␈↓↓p1␈↓ and ␈↓↓p2␈↓ in the case of products.
␈↓ ↓H␈↓ The␈α∀predicates␈α∪and␈α∀analytic␈α∀operations␈α∪comprise␈α∀the␈α∀␈↓↓abstract␈α∪analytic␈α∀syntax␈↓␈α∀of␈α∪these
␈↓ ↓H␈↓expressions,␈αe.g.␈αthey␈αallow␈αus␈αto␈αanalyse␈αthe␈αexpressions.␈α The␈αanalytic␈αsyntax␈αprovides␈αsufficient
␈↓ ↓H␈↓information␈α⊂to␈α∂allow␈α⊂us␈α⊂to␈α∂write␈α⊂an␈α⊂interpreter␈α∂(evaluator)␈α⊂thus␈α⊂defining␈α∂the␈α⊂semantics␈α⊂of␈α∂our
␈↓ ↓H␈↓expression language. Thus we have
␈↓ ↓H␈↓␈↓ βx␈↓↓value(e,␈↓πx␈↓↓) ← ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αif␈↓↓ isconst(e) ␈↓αthen␈↓↓ val(e) ␈↓
␈↓ ↓H␈↓1.1)␈↓ ∧8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ isvar(e) ␈↓αthen␈↓↓ c(e,␈↓πx␈↓↓)␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ issum(e) ␈↓αthen␈↓↓ value(s1(e),␈↓πx␈↓↓) + value(s2(e),␈↓πx␈↓↓)␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ isprod(e) ␈↓αthen␈↓↓ value(p1(e),␈↓πx␈↓↓) ␈↓π*␈↓↓ value(p2(e),␈↓πx␈↓↓)␈↓
␈↓ ↓H␈↓where␈α␈↓↓val(e)␈↓␈αgives␈αthe␈αnumerical␈αvalue␈αof␈αan␈αexpression␈α␈↓↓e␈↓␈αrepresenting␈αa␈αconstant,␈α␈↓↓c(e,␈↓πx␈↓↓)␈↓␈αgives␈αthe
␈↓ ↓H␈↓value␈α
of␈α∞the␈α
variable␈α∞␈↓↓e␈↓␈α
in␈α∞the␈α
state␈α∞vector␈α
␈↓πx␈↓␈α∞and␈α
"+"␈α∞and␈α
"␈↓π*␈↓"␈α∞are␈α
some␈α∞binary␈α
operations.␈α∞ (It␈α
is
␈↓ ↓H␈↓natural␈αto␈α
regard␈α"+"␈αand␈α
"␈↓π*␈↓"␈αas␈αan␈α
operations␈αthat␈αresembles␈α
addition␈αand␈αmultiplication␈α
of␈αreal
␈↓ ↓H␈↓numbers, but our we shall need only a few properties of these operations.)
␈↓ ↓H␈↓ The␈α␈↓αsynthetic␈αoperation␈↓s␈αcolumn␈αof␈αtable 1␈αsays␈αthat␈αsums␈αcan␈αbe␈αconstructed␈αfrom␈αa␈αpair
␈↓ ↓H␈↓of␈αexpressions␈αusing␈αthe␈α
operation␈α␈↓↓mksum␈↓␈αand␈αsimilarly␈αproducts␈α
can␈αbe␈αconstructed␈αfrom␈α
a␈αpair
␈↓ ↓H␈↓of␈α
expression␈αusing␈α
␈↓↓mkprod.␈↓␈α These␈α
operations␈α
form␈αthe␈α
␈↓↓abstract␈αsynthetic␈α
syntax␈↓␈α
of␈αexpressions,
␈↓ ↓H␈↓e.g. they allow us to construct new expressions from old ones.
␈↓ ↓H␈↓ Using␈α
the␈α
abstract␈α
syntax␈α
functions␈α
and␈α
predicates␈α
we␈α
can␈α
write␈α
a␈α
function␈α
that␈α
simplifies
␈↓ ↓H␈↓an expression by
␈↓ ↓H␈↓␈↓ βλ1.1. removing 0's from sums,
␈↓ ↓H␈↓␈↓ βλ1.2. removing 1's from products,
␈↓ ↓H␈↓␈↓ βλ1.3. replacing a product containing 0 as a factor by 0.
␈↓ ↓H␈↓We␈α⊃will␈α⊃assume␈α⊃the␈α⊃constants␈α⊃0␈α⊃and␈α⊃1␈α⊂are␈α⊃represented␈α⊃by␈α⊃"0"␈α⊃and␈α⊃"1",␈α⊃for␈α⊃simplicity.␈α⊂ Then
␈↓ ↓H␈↓␈↓↓simplify␈↓ is given by
␈↓ ↓H␈↓␈↓ β8␈↓↓simplify(e) ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓αif␈↓↓ isvar(e) ∨ isconst(e) ␈↓αthen␈↓↓ e␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ issum(e) ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ {simplify(s1(e)),simplify(s2(e))} ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ [λe1 e2. ␈↓αif␈↓↓ e1=0 ␈↓αthen␈↓↓ e2 ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ e2=0 ␈↓αthen␈↓↓ e1 ␈↓
␈↓ ↓H␈↓1.2)␈↓ β8␈↓↓ ␈↓αelse␈↓↓ mksum(e1,e2)] ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ isprod(e) ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ {simplify(p1(e)),simplify(p2(e))} ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ [λe1 e2. ␈↓αif␈↓↓ e1=0 ∨ e2=0 ␈↓αthen␈↓↓ 0 ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ e1=1 ␈↓αthen␈↓↓ e2 ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ e2=1 ␈↓αthen␈↓↓ e1 ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓αelse␈↓↓ mkprod(e1,e2)] ␈↓
␈↓ ↓H␈↓104␈↓ ¬wChapter VI␈↓ H
␈↓ ↓H␈↓ Suppose␈α∪we␈α∀would␈α∪like␈α∪to␈α∀prove␈α∪something␈α∪about␈α∀our␈α∪functions␈α∪on␈α∀expressions.␈α∪ For
␈↓ ↓H␈↓example␈αwe␈αwould␈αlike␈αto␈αknow␈αthat␈αthe␈αsimplifier␈αwill␈αindeed␈αterminate␈α(e.g.␈αthere␈αare␈αno␈α
infinite
␈↓ ↓H␈↓expressions).␈α Then␈αwe␈α
need␈α and␈αa␈αway␈α
of␈αexpressing␈αthe␈αfact␈α
that␈αthe␈αonly␈α
expressions␈αallowed
␈↓ ↓H␈↓are␈αthose␈αthat␈αcan␈αbe␈αconstructed␈αfrom␈αvariables␈αand␈αconstants␈αby␈αa␈αfinite␈αnumber␈αof␈αapplications
␈↓ ↓H␈↓of␈α∩the␈α∪sum␈α∩and␈α∪product␈α∩constructions.␈α∪ The␈α∩following␈α∪schema␈α∩is␈α∪an␈α∩induction␈α∪principle␈α∩for
␈↓ ↓H␈↓expressions essentially expressing this fact.
␈↓ ↓H␈↓␈↓ αλ Suppose␈α
␈↓πF␈↓␈α
is␈α
a␈αpredicate␈α
applicable␈α
to␈α
expressions,␈α
and␈αsuppose␈α
that␈α
for␈α
all␈αexpressions␈α
␈↓↓e,␈↓
␈↓ ↓H␈↓␈↓ αλwe have
␈↓ ↓H␈↓↓␈↓ ∧8isconst(e) ⊃ ␈↓πF␈↓↓(e) ␈↓and␈↓↓
␈↓ ↓H␈↓↓1.3)␈↓ ∧8isvar(e) ⊃ ␈↓πF␈↓↓(e) ␈↓and␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8issum(e) ∧ ␈↓πF␈↓↓(s1(e)) ∧ ␈↓πF␈↓↓(s2(e)) ⊃ ␈↓πF␈↓↓(e) ␈↓and␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8isprod(e) ∧ ␈↓πF␈↓↓(p1(e)) ∧ ␈↓πF␈↓↓(p2(e)) ⊃ ␈↓πF␈↓↓(e).
␈↓ ↓H␈↓ Then we may conclude that ␈↓πF␈↓(e) is true for all expressions ␈↓↓e.␈↓
␈↓ ↓H␈↓Using this principle we can show that the simplifier terminates by the techniques of Chapter III.
␈↓ ↓H␈↓ Now␈αsuppose␈αwe␈αwould␈α
like␈αto␈αshow␈αthat␈αthe␈α
simplifier␈αpreserves␈αthe␈αvalue␈α
of␈αexpressions.
␈↓ ↓H␈↓Then␈α∃we␈α∃will␈α∀need␈α∃some␈α∃facts␈α∃about␈α∀the␈α∃relations␈α∃satisfied␈α∀by␈α∃the␈α∃analytic␈α∃and␈α∀synthetic
␈↓ ↓H␈↓components of the syntax. Thus for expressions ␈↓↓e,␈↓ ␈↓↓e1,␈↓ ␈↓↓e2␈↓ we have
␈↓ ↓H␈↓␈↓ ∧8␈↓↓issum(mksum(e1,e2)) ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓s1(mksum(e1,e2))=e1 ␈↓
␈↓ ↓H␈↓1.4)␈↓ ∧8␈↓↓s2(mksum(e1,e2))=e2 ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓issum(e) ⊃ e=mksum(s1(e),s2(e)) ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓isprod(mkprod(e1,e2)) ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓p1(mkprod(e1,e2))=e1 ␈↓
␈↓ ↓H␈↓1.5)␈↓ ∧8␈↓↓p2(mkprod(e1,e2))=e2 ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓isprod(e) ⊃ e=mkprod(p1(e),p2(e)) ␈↓
␈↓ ↓H␈↓Using␈α⊃these␈α⊃relations,␈α⊃the␈α⊃principle␈α⊂of␈α⊃induction,␈α⊃and␈α⊃some␈α⊃simple␈α⊂properties␈α⊃of␈α⊃the␈α⊃+␈α⊃and␈α⊂␈↓π*␈↓
␈↓ ↓H␈↓operations involving 0 and 1 we can show that
␈↓ ↓H␈↓1.6)␈↓ ¬β␈↓↓value(e,␈↓πx␈↓↓)=value(simplify(e),␈↓πx␈↓↓)␈↓
␈↓ ↓H␈↓far all expressions ␈↓↓e␈↓ and state vectors ␈↓πx␈↓.
␈↓ ↓H␈↓αExample 2. Abstract and Concrete Syntax of LISP terms.
␈↓ ↓H␈↓ Now␈αwe␈αwill␈αgive␈αthe␈αabstract␈αand␈αconcrete␈αanalytic␈αsyntax␈αof␈αpure␈α(functional)␈αLISP␈αterms.
␈↓ ↓H␈↓We␈αwill␈α
see␈αin␈α
the␈αcase␈α
of␈αLISP␈α
the␈αconcrete␈α
syntax␈αis␈α
very␈αclose␈α
to␈αthe␈α
abstract␈αsyntax.␈α
This␈αis
␈↓ ↓H␈↓one␈αreason␈αthat␈αit␈αis␈αso␈αeasy␈αto␈αwrite␈αLISP␈αprograms␈αto␈αmanipulate␈αLISP␈αprograms.␈α We␈αwill␈αalso
␈↓ ↓H␈↓introduce in this example additional techniques which are useful for defining abstract syntax.
␈↓ ↓H␈↓ A␈α
pure␈α
LISP␈α
term␈α
is␈αessentially␈α
any␈α
term␈α
that␈α
can␈α
occur␈αat␈α
the␈α
right␈α
hand␈α
side␈α
of␈αa␈α
defining
␈↓ ↓H␈↓equation␈α⊂as␈α⊂described␈α∂in␈α⊂Chapter␈α⊂I␈α∂(excluding␈α⊂labels).␈α⊂ Thus␈α∂LISP␈α⊂terms␈α⊂are␈α⊂either␈α∂variables,
␈↓ ↓H␈↓␈↓ ¬wChapter VI␈↓ ≠105
␈↓ ↓H␈↓constants,␈α
conditional␈α
terms,␈αlist␈α
terms,␈α
or␈αapplication␈α
terms,␈α
denoted␈αby␈α
␈↓↓lvar,␈↓␈α
␈↓↓lconst,␈↓␈α
␈↓↓lcond,␈↓␈α␈↓↓llist␈↓
␈↓ ↓H␈↓and ␈↓↓lappl␈↓ respectively.
␈↓ ↓H␈↓ We␈αwill␈αuse␈αsome␈αfurther␈αclasses␈αof␈αexpressions␈αwhich␈αare␈αnot␈αLISP␈αterms␈αbut␈αare␈αuseful␈αin
␈↓ ↓H␈↓analyzing␈α⊃the␈α⊃composite␈α⊃terms.␈α⊂ These␈α⊃are␈α⊃the␈α⊃classes␈α⊂of␈α⊃function␈α⊃expressions:␈α⊃basic␈α⊂functions
␈↓ ↓H␈↓(␈↓↓lbfun),␈↓␈α⊃defined␈α⊂functions␈α⊃(␈↓↓ldfun),␈↓␈α⊃and␈α⊂lambda␈α⊃expressions␈α⊃(␈↓↓llamb)␈↓␈α⊂ that␈α⊃occur␈α⊃in␈α⊂application
␈↓ ↓H␈↓terms.
␈↓ ↓H␈↓ Finally␈αsome␈αof␈αthe␈αcomponents␈αof␈αthe␈αexpressions␈αwill␈αbe␈αlists␈αof␈αexpressions.␈α We␈αwill␈αuse
␈↓ ↓H␈↓␈↓↓car,␈↓␈α␈↓↓cdr␈↓␈αand␈α
␈↓↓null␈↓␈αto␈αanalyse␈αthe␈α
lists␈αof␈αexpressions.␈α
It␈αshould␈αbe␈αemphasized␈α
that␈αthis␈αuse␈α
is␈αin
␈↓ ↓H␈↓an␈α∞abstract␈α∞sense␈α∞and␈α∞does␈α∞not␈α∞depend␈α
on␈α∞any␈α∞properties␈α∞of␈α∞a␈α∞particular␈α∞representation␈α∞of␈α
lists.
␈↓ ↓H␈↓There␈α
will␈α
be␈α
three␈α
special␈αkinds␈α
of␈α
lists:␈α
the␈α
list␈α
of␈αpairs␈α
of␈α
terms␈α
in␈α
a␈α
conditional␈αterm␈α
(␈↓↓lpairlist),␈↓
␈↓ ↓H␈↓the␈α∂list␈α∂of␈α∂arguments␈α∂in␈α∂an␈α∂application␈α∂term␈α∂(␈↓↓ltermlist),␈↓␈α∂and␈α∂the␈α∂list␈α∂of␈α∂variables␈α∂in␈α∂a␈α∞lambda
␈↓ ↓H␈↓expression (␈↓↓lvarlist).␈↓
␈↓ ↓H␈↓ The abstract analytic syntax of LISP terms is given in table 2.
␈↓ ↓H␈↓↓␈↓ βx␈↓αpredicate␈↓ ε8associated functions␈↓↓
␈↓ ↓H␈↓↓␈↓ βxlconst(e)
␈↓ ↓H␈↓↓␈↓ βxlvar(e)
␈↓ ↓H␈↓↓␈↓ βxlbfun(e)
␈↓ ↓H␈↓↓␈↓ βxldfun(e)
␈↓ ↓H␈↓↓␈↓ βxnull(e)
␈↓ ↓H␈↓↓␈↓ βxlcond(e)␈↓ ε8pairl(e)
␈↓ ↓H␈↓↓␈↓ βxllist(e)␈↓ ε8terml(e)
␈↓ ↓H␈↓↓␈↓ βxlappl(e)␈↓ ε8funx(e), argl(e)
␈↓ ↓H␈↓↓␈↓ βxllamb(e)␈↓ ε8varl(e), body(e)
␈↓ ↓H␈↓↓␈↓ βxlpairlist(e)␈↓ ε8ifpart(e), thenpart(e), elsepart(e)
␈↓ ↓H␈↓↓␈↓ βxltermlist␈↓ ε8car, cdr
␈↓ ↓H␈↓↓␈↓ βxlvarlist␈↓ ε8car, cdr
␈↓ ↓H␈↓␈↓ ε∩␈↓αTable 2.␈↓
␈↓ ↓H␈↓ In␈αthe␈αusual␈α
S-expression␈αrepresentation␈αof␈α
LISP␈αterms␈α(internal␈α
form)␈αwe␈αwould␈α
have␈αfor
␈↓ ↓H␈↓the basic predicates the following definitions
␈↓ ↓H␈↓␈↓ αx␈↓↓lconst x ← [␈↓αat|␈↓↓x ∧ [numberp x ∨ x=␈↓¬T␈↓↓ ∨ x=␈↓¬NIL␈↓↓]] ∨ ␈↓αa|␈↓↓x=␈↓¬QUOTE ␈↓↓␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓lvar x ← ␈↓αat|␈↓↓x ∧ ¬[numberp x ∨ x=␈↓¬T␈↓↓ ∨ x=␈↓¬NIL␈↓↓] ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓lbfun(e) ← [e=␈↓¬CAR ␈↓↓∨ e=␈↓¬CDR ␈↓↓∨ e=␈↓¬CONS ␈↓↓∨ e=␈↓¬ATOM ␈↓↓∨ e=␈↓¬EQ␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓ldfun(e) ← ␈↓αat|␈↓↓e ∧ ¬[numberp(e) ∨ e=␈↓¬T␈↓↓ ∨ e=␈↓¬NIL␈↓↓ ∨ lbfun(e)] ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓null(e) ← ␈↓αn|␈↓↓e ␈↓
␈↓ ↓H␈↓The concrete syntax for conditional terms and the related auxiliary expressions is given by
␈↓ ↓H␈↓␈↓ αx␈↓↓lcond(e) ← ␈↓αa|␈↓↓e=␈↓¬COND ␈↓↓∧ lpairlist(pairl(e))␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓pairl(e) ← ␈↓αd|␈↓↓e ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓lpairlist(e) ← ␈↓αn|␈↓↓e ∨ [lterm(ifpart(e)) ∧ lterm(thenpart(e) ∧ lpairlist(elsepart(e))]␈↓
␈↓ ↓H␈↓106␈↓ ¬wChapter VI␈↓ H
␈↓ ↓H␈↓␈↓ αx␈↓↓ifpart(e) ← ␈↓αaa|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓thenpart(e) ← ␈↓αada|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓elsepart(e) ← ␈↓αd|␈↓↓e␈↓
␈↓ ↓H␈↓For list terms we have
␈↓ ↓H␈↓␈↓ αx␈↓↓llist(e) ← ␈↓αa|␈↓↓e=␈↓¬LIST ␈↓↓∧ ltermlist(terml(e))␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓terml(e) ← ␈↓αd|␈↓↓e ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓ltermlist(e) ← ␈↓αn|␈↓↓e ∨ [lterm(car(e)) ∧ ltermlist(cdr(e))]␈↓
␈↓ ↓H␈↓The concrete syntax for application terms and lambda expression is the following
␈↓ ↓H␈↓␈↓ αx␈↓↓lappl(e) ← [lbfun(funx(e)) ∨ ldfun(funx(e)) ∨ llamb(funx(e))] ∧ ltermlist(argl(e))␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓funx(e) ← ␈↓αa|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓argl(e) ← ␈↓αd|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓llamb(e) ← ␈↓αa|␈↓↓e=␈↓¬LAMBDA ␈↓↓∧ lvarlist(varl(e)) ∧ lterm(body(e))␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓varl(e) ← ␈↓αad|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓body(e) ← ␈↓αadd|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓lvarlist(e)← ␈↓αn|␈↓↓e ∨ [lvar(car(e)) ∧ lvarlist(cdr(e))]␈↓
␈↓ ↓H␈↓α␈↓ ε
Exercises
␈↓ ↓H␈↓1.␈α∂Prove␈α∂the␈α∂statements␈α∂made␈α∂about␈α∂the␈α∂expression␈α∂simplifier␈α∂in␈α∂example␈α∂1.␈α∂ That␈α∂is,␈α∂prove␈α∞it
␈↓ ↓H␈↓terminates by defining a predicate ␈↓↓isexp␈↓ such that
␈↓ ↓H␈↓␈↓ β␈␈↓↓isexp(e) ≡ isvar(e) ∨ isconst(e) ∨ issum(e) ∨ isprod(e)␈↓
␈↓ ↓H␈↓and␈αshowing␈α
that␈α␈↓↓isexp(simplify(e))␈↓␈α
for␈αall␈α
expressions␈α␈↓↓e.␈↓␈α
Then␈αshow␈α
that␈αthe␈α
statement␈α(1.6)␈α
hold
␈↓ ↓H␈↓for all expressions.
␈↓ ↓H␈↓2.␈α Work␈αout␈αthe␈αsynthetic␈αsyntax␈αfor␈αLISP␈αterms␈αand␈αstate␈αrelations␈αfor␈αeach␈αconstruct␈αanalagous
␈↓ ↓H␈↓to those given for the expressions in example 1. State an induction principle for LISP terms.
␈↓ ↓H␈↓2. ␈↓αCorrectness of a Compiler for Arithmetic Expressions␈↓
␈↓ ↓H␈↓ In␈α⊃the␈α⊃following␈α⊃sections␈α⊃we␈α⊃prove␈α⊂the␈α⊃correctness␈α⊃of␈α⊃a␈α⊃simple␈α⊃compiling␈α⊃algorithm␈α⊂for
␈↓ ↓H␈↓compiling␈α
arithmetic␈α
expressions␈α
into␈α
machine␈α
language.␈α
The␈α
presentation␈α
is␈α
adapted␈α
from␈αthat
␈↓ ↓H␈↓of McCarthy and Painter[1967].
␈↓ ↓H␈↓ We␈αbegin␈αwith␈αa␈αbrief␈αoutline␈αof␈αthe␈αmethods␈αused␈αto␈αformalize␈αthe␈αproblem.␈α The␈αanalytic
␈↓ ↓H␈↓abstract␈α∪syntax␈α∩is␈α∪given␈α∩for␈α∪both␈α∪the␈α∩arithmetic␈α∪expressions␈α∩(source␈α∪language)␈α∪and␈α∩machine
␈↓ ↓H␈↓language␈α∞programs␈α∞(object␈α∞language).␈α∞ An␈α∞interpreter␈α∞defined␈α∞in␈α∞terms␈α∞of␈α∞the␈α∞analytic␈α∞syntax␈α
is
␈↓ ↓H␈↓given␈αfor␈αeach␈αlanguage.␈α The␈αtwo␈αinterpreters␈αare␈αwritten␈αin␈αthe␈αsame␈αlanguage␈α and␈αthus␈αdefine
␈↓ ↓H␈↓the␈αsemantics␈αof␈α
the␈αtwo␈αlanguages␈αin␈α
common␈αterms.␈α The␈αinterpreters␈α
each␈αmake␈αuse␈αof␈α
a␈α␈↓↓state␈↓
␈↓ ↓H␈↓␈↓↓vector␈↓␈αto␈α
determine␈αthe␈α
values␈αof␈α
variables␈α(in␈αthe␈α
expression␈αlanguage␈α
case)␈αor␈α
to␈αstore␈αcontents␈α
of
␈↓ ↓H␈↓␈↓ ¬wChapter VI␈↓ ≠107
␈↓ ↓H␈↓registers␈α∂(in␈α⊂the␈α∂machine␈α⊂language␈α∂case).␈α⊂ The␈α∂synthetic␈α∂syntax␈α⊂is␈α∂also␈α⊂given␈α∂for␈α⊂the␈α∂machine
␈↓ ↓H␈↓language␈αand␈αthe␈α
compiler␈αis␈αdefined␈αin␈α
terms␈αof␈αthe␈αanalytic␈α
syntax␈αof␈αthe␈αsource␈α
language␈αand
␈↓ ↓H␈↓the␈αsynthetic␈αsyntax␈αof␈α
the␈αobject␈αlanguage.␈α Thus␈αthe␈α
compiler␈αbreaks␈αdown␈αterms␈αin␈α
the␈αsource
␈↓ ↓H␈↓language␈αand␈α
rebuilds␈αthem␈αin␈α
the␈αobject␈αlanguage.␈α
To␈αshow␈α
that␈αthe␈αcompiler␈α
is␈αcorrect␈αwe␈α
must
␈↓ ↓H␈↓show␈αthat␈αfor␈αan␈αexpression␈α
␈↓↓e␈↓␈αthe␈αinterpretation␈αof␈α␈↓↓e␈↓␈α
by␈αthe␈αexpression␈αinterpreter␈αgives␈αthe␈α
same
␈↓ ↓H␈↓answer␈αas␈αthe␈α
execution␈αof␈αthe␈α
compiled␈αcode␈αfor␈α
␈↓↓e␈↓␈αby␈αthe␈α
machine␈αlanguage␈αinterpreter,␈α
assuming
␈↓ ↓H␈↓that they start out with the same values for all of the variables in ␈↓↓e.␈↓
␈↓ ↓H␈↓ The␈α⊃expressions␈α∩of␈α⊃our␈α⊃source␈α∩language␈α⊃are␈α⊃a␈α∩subclass␈α⊃of␈α⊃those␈α∩described␈α⊃in␈α∩␈↓π∞␈↓1.␈α⊃ The
␈↓ ↓H␈↓computer␈α∂language␈α∂into␈α∂which␈α∞these␈α∂expressions␈α∂are␈α∂compiled␈α∞is␈α∂for␈α∂a␈α∂single␈α∂address␈α∞computer
␈↓ ↓H␈↓with␈α∞an␈α∞accumulator,␈α∞called␈α∞␈↓↓ac,␈↓␈α∞and␈α∞four␈α∞instructions:␈α∞␈↓↓li␈↓␈α∞(load␈α∞immediate),␈α∞␈↓↓load,␈↓␈α∞␈↓↓sto␈↓␈α∞(store)␈α
and
␈↓ ↓H␈↓␈↓↓add.␈↓␈α
(Our␈α
simple␈α
language␈α
does␈α
not␈αrequire␈α
the␈α
use␈α
of␈α
jump␈α
instructions.)␈α
The␈αcompiler␈α
produces
␈↓ ↓H␈↓code␈α⊃that␈α⊂computes␈α⊃the␈α⊂value␈α⊃of␈α⊂the␈α⊃expression␈α⊂being␈α⊃compiled␈α⊂and␈α⊃leaves␈α⊂this␈α⊃value␈α⊃in␈α⊂the
␈↓ ↓H␈↓accumulator.␈α The␈αabove␈α
expression␈αis␈αcompiled␈α
into␈αcode␈αwhich␈α
in␈αassembly␈αlanguage␈αmight␈α
look
␈↓ ↓H␈↓as follows:
␈↓ ↓H␈↓↓␈↓ ¬hload␈↓ εHx
␈↓ ↓H␈↓↓␈↓ ¬hsto␈↓ εHt
␈↓ ↓H␈↓↓␈↓ ¬hli␈↓ εH3
␈↓ ↓H␈↓↓␈↓ ¬hadd␈↓ εHt
␈↓ ↓H␈↓↓␈↓ ¬hsto␈↓ εHt
␈↓ ↓H␈↓↓␈↓ ¬hload␈↓ εHx
␈↓ ↓H␈↓↓␈↓ ¬hsto␈↓ εHt + 1
␈↓ ↓H␈↓↓␈↓ ¬hload␈↓ εHy
␈↓ ↓H␈↓↓␈↓ ¬hsto␈↓ εHt + 2
␈↓ ↓H␈↓↓␈↓ ¬hli␈↓ εH2
␈↓ ↓H␈↓↓␈↓ ¬hadd␈↓ εHt + 2
␈↓ ↓H␈↓↓␈↓ ¬hadd␈↓ εHt + 1
␈↓ ↓H␈↓↓␈↓ ¬hadd␈↓ εHt
␈↓ ↓H␈↓Again␈α∂because␈α∂we␈α∂are␈α∂using␈α∞abstract␈α∂syntax␈α∂there␈α∂is␈α∂no␈α∞commitment␈α∂to␈α∂a␈α∂precise␈α∂form␈α∂for␈α∞the
␈↓ ↓H␈↓object code.
␈↓ ↓H␈↓3. ␈↓αThe source language. ␈↓
␈↓ ↓H␈↓ As␈α
mentioned␈α
above␈α
the␈αsource␈α
language␈α
is␈α
a␈α
subset␈αof␈α
the␈α
expression␈α
language␈αdescribed␈α
in
␈↓ ↓H␈↓␈↓π∞␈↓1.␈α In␈αparticular,␈αwe␈αwill␈αconsider␈αexpressions␈αthat␈αare␈αvariables,␈αconstants␈αor␈αsums.␈α Products␈αare
␈↓ ↓H␈↓omitted␈α
as␈α
they␈α
introduce␈α
nothing␈α
new␈α
into␈αthe␈α
problem␈α
and␈α
serve␈α
only␈α
to␈α
make␈αeverything␈α
longer.
␈↓ ↓H␈↓Thus␈α∀we␈α∪will␈α∀use␈α∪the␈α∀relevant␈α∪parts␈α∀of␈α∀table␈α∪1␈α∀for␈α∪the␈α∀abstract␈α∪analytic␈α∀syntax␈α∀of␈α∪source
␈↓ ↓H␈↓expressions.␈α
The␈α
semantics␈α
is␈α
given␈α
by␈α
the␈α
interpreter␈α
␈↓↓value␈↓␈α
(1.1)␈α
by␈α
simply␈α
ignoring␈α
the␈α
␈↓↓isprod␈↓
␈↓ ↓H␈↓clause.
␈↓ ↓H␈↓ For␈α∩proving␈α∩the␈α∩correctness␈α∪of␈α∩compilers,␈α∩we␈α∩don't␈α∪need␈α∩a␈α∩synthetic␈α∩syntax␈α∪for␈α∩source
␈↓ ↓H␈↓language␈α∀expressions␈α∪since␈α∀the␈α∪interpreter␈α∀and␈α∀compiler␈α∪use␈α∀only␈α∪the␈α∀analytic␈α∀syntax.␈α∪ The
␈↓ ↓H␈↓induction␈αprinciple␈αfor␈αthis␈αsimpler␈αclass␈αof␈αexpressions␈α is␈αobtained␈αby␈αdeleting␈αthe␈α␈↓↓isprod␈↓␈αclause
␈↓ ↓H␈↓from the induction hypothesis (1.3). Thus
␈↓ ↓H␈↓108␈↓ ¬wChapter VI␈↓ H
␈↓ ↓H␈↓␈↓ αλ If ␈↓πF␈↓ is a predicate applicable to expressions, and if for all expressions ␈↓↓e,␈↓ we have
␈↓ ↓H␈↓↓␈↓ αλ␈↓ ∧8isconst(e) ⊃ ␈↓πF␈↓↓(e) ␈↓and␈↓↓
␈↓ ↓H␈↓↓␈↓ αλ3.1)␈↓ ∧8isvar(e) ⊃ ␈↓πF␈↓↓(e) ␈↓and␈↓↓
␈↓ ↓H␈↓↓␈↓ αλ␈↓ ∧8issum(e) ∧ ␈↓πF␈↓↓(s1(e)) ∧ ␈↓πF␈↓↓(s2(e)) ⊃ ␈↓πF␈↓↓(e).
␈↓ ↓H␈↓␈↓ αλThen we may conclude that ␈↓πF␈↓(e) is true for all expressions ␈↓↓e.␈↓
␈↓ ↓H␈↓4. ␈↓αThe object language.␈↓
␈↓ ↓H␈↓ We␈αmust␈αgive␈αboth␈αthe␈αanalytic␈αand␈αsynthetic␈αsyntaxes␈αfor␈αthe␈αobject␈αlanguage␈αbecause␈αthe
␈↓ ↓H␈↓interpreter␈α⊂defining␈α⊂its␈α⊂semantics␈α⊂uses␈α⊂the␈α⊂analytic␈α⊂syntax␈α⊂and␈α⊂the␈α⊂compiler␈α⊂uses␈α⊃the␈α⊂synthetic
␈↓ ↓H␈↓syntax. The analytic and synthetic syntaxes for instructions are given in the table 3.
␈↓ ↓H␈↓↓␈↓ α8␈↓αoperation␈↓ ∧_predicate␈↓ ¬xanalytic operation␈↓ λXsynthetic operation
␈↓ ↓H␈↓α␈↓ αHli␈↓ β_␈↓πa␈↓α␈↓ ∧_isli(s)␈↓ ε8arg(s)␈↓ _mkli(␈↓πa␈↓α)
␈↓ ↓H␈↓α␈↓ αHload␈↓ β_x␈↓ ∧_isload(s)␈↓ ε8adr(s)␈↓ _mkload(x)
␈↓ ↓H␈↓α␈↓ αHsto␈↓ β_x␈↓ ∧_issto(s)␈↓ ε8adr(s)␈↓ _mksto(x)
␈↓ ↓H␈↓α␈↓ αHadd␈↓ β_x␈↓ ∧_isadd(s)␈↓ ε8adr(s)␈↓ _mkadd(x)
␈↓ ↓H␈↓␈↓ ε∩␈↓αTable 3.␈↓
␈↓ ↓H␈↓ A␈αprogram␈αis␈αa␈αlist␈αof␈αinstructions␈αand␈α␈↓↓null(p)␈↓␈αasserts␈αthat␈α␈↓↓p␈↓␈αis␈αthe␈αnull␈αlist.␈α If␈αthe␈α
program
␈↓ ↓H␈↓␈↓↓p␈↓␈α⊃is␈α∩not␈α⊃null␈α∩then␈α⊃␈↓↓first(p)␈↓␈α∩gives␈α⊃the␈α⊃first␈α∩instruction␈α⊃and␈α∩␈↓↓rest(p)␈↓␈α⊃gives␈α∩the␈α⊃list␈α∩of␈α⊃remaining
␈↓ ↓H␈↓instructions.␈α We␈αshall␈αuse␈αthe␈αoperation␈α␈↓↓p1*p2␈↓␈αto␈αdenote␈αthe␈αprogram␈αobtained␈αby␈αappending␈α␈↓↓p2␈↓
␈↓ ↓H␈↓onto␈αthe␈αend␈αof␈α␈↓↓p1.␈↓␈α Since␈αwe␈αhave␈αonly␈αone␈αlevel␈αof␈αlist␈αwe␈αcan␈αidentify␈αa␈αsingle␈αinstruction␈αwith
␈↓ ↓H␈↓a program that has just one instruction.
␈↓ ↓H␈↓ The synthetic and analytic syntaxes of instructions are related by the following.
␈↓ ↓H␈↓↓␈↓ βxisli(mkli(␈↓πa␈↓↓))
␈↓ ↓H␈↓↓␈↓ βxarg(mkli(␈↓πa␈↓↓)) = ␈↓πa␈↓↓
␈↓ ↓H␈↓↓4.1)␈↓ βxisli(s) ⊃ s = mkli(arg(s))
␈↓ ↓H␈↓↓␈↓ βxnull(rest(mkli(␈↓πa␈↓↓)))
␈↓ ↓H␈↓↓␈↓ βxisli(s) ⊃ first(s) = s
␈↓ ↓H␈↓↓␈↓ βxisload(mkload(x))
␈↓ ↓H␈↓↓␈↓ βxx = adr(mkload(x))
␈↓ ↓H␈↓↓4.2)␈↓ βxisload(x) ⊃ x = mkload(adr(x))
␈↓ ↓H␈↓↓␈↓ βxnull(rest(mkload(x)))
␈↓ ↓H␈↓↓␈↓ βxisload(s) ⊃ first(s) = s
␈↓ ↓H␈↓␈↓ ¬wChapter VI␈↓ ≠109
␈↓ ↓H␈↓↓␈↓ βxissto(mksto(x))
␈↓ ↓H␈↓↓␈↓ βxx = adr(mksto(x))
␈↓ ↓H␈↓↓4.3)␈↓ βxissto(s) ⊃ s = mksto(adr(s))
␈↓ ↓H␈↓↓␈↓ βxnull(rest(mksto(x)))
␈↓ ↓H␈↓↓␈↓ βxissto(s) ⊃ first(s) = s
␈↓ ↓H␈↓↓␈↓ βxisadd(mkadd(x))
␈↓ ↓H␈↓↓␈↓ βxx = adr(mkadd(x))
␈↓ ↓H␈↓↓4.4)␈↓ βxisadd(s) ⊃ s = mkadd(adr(s))
␈↓ ↓H␈↓↓␈↓ βxnull(rest(mkadd(x)))
␈↓ ↓H␈↓↓␈↓ βxisadd(s) ⊃ first(s) = s
␈↓ ↓H␈↓↓␈↓ βx¬null(p) ⊃ p = first(p) * rest(p),
␈↓ ↓H␈↓↓4.5)␈↓ βx¬null(p1) ∧ null(rest(p1)) ⊃ p1 = first(p1*p2)
␈↓ ↓H␈↓↓␈↓ βxnull(p1*p2) ≡ null(p1) ∧ null(p2).
␈↓ ↓H␈↓The␈α⊃*␈α⊃operation␈α⊃is␈α⊃associative.␈α⊃(The␈α⊃somewhat␈α⊃awkward␈α⊃form␈α⊃of␈α⊃these␈α⊃relations␈α⊃comes␈α⊃from
␈↓ ↓H␈↓having␈α⊂a␈α∂general␈α⊂concatenation␈α∂operation␈α⊂rather␈α∂than␈α⊂just␈α∂an␈α⊂operation␈α∂that␈α⊂prefixes␈α⊂a␈α∂single
␈↓ ↓H␈↓instruction onto a program.)
␈↓ ↓H␈↓ A␈α⊂state␈α⊃vector␈α⊂for␈α⊃a␈α⊂machine␈α⊃gives,␈α⊂for␈α⊃each␈α⊂register␈α⊃in␈α⊂the␈α⊃machine,␈α⊂its␈α⊃contents.␈α⊂ We
␈↓ ↓H␈↓include␈αthe␈αaccumulator␈αdenoted␈αby␈α␈↓↓ac␈↓␈αas␈αa␈αregister.␈αAssociatedwith␈αstate␈αvectors␈αare␈αthe␈αfunctions
␈↓ ↓H␈↓␈↓↓a␈↓ (for assign) and ␈↓↓c␈↓ (for contents). Thus
␈↓ ↓H␈↓ 1. ␈↓↓c(x,␈↓πh␈↓↓)␈↓ denotes the value of the contents of register ␈↓↓x␈↓ in machine state ␈↓πh␈↓.
␈↓ ↓H␈↓ 2.␈α
␈↓↓a(x,␈↓πa␈↓↓,␈↓πh␈↓↓)␈↓␈α
denotes␈α∞the␈α
state␈α
vector␈α
that␈α∞is␈α
obtained␈α
from␈α
the␈α∞state␈α
vector␈α
␈↓πh␈↓␈α∞by␈α
changing
␈↓ ↓H␈↓the contents of register ␈↓↓x␈↓ to ␈↓πa␈↓ leaving the other registers unaffected.
␈↓ ↓H␈↓The relations satisfied by these functions are:
␈↓ ↓H␈↓␈↓ βx␈↓↓ c(x,a(y,␈↓πa␈↓↓,␈↓πh␈↓↓)) = ␈↓αif␈↓↓ x = y ␈↓αthen␈↓↓ ␈↓πa␈↓↓ ␈↓αelse␈↓↓ c(x,␈↓πh␈↓↓),␈↓
␈↓ ↓H␈↓4.6)␈↓ βx␈↓↓a(x,␈↓πa␈↓↓,a(y,␈↓πa␈↓↓,␈↓πh␈↓↓)) = ␈↓αif␈↓↓ x = y ␈↓αthen␈↓↓ a(x,␈↓πa␈↓↓,␈↓πh␈↓↓) ␈↓αelse␈↓↓ a(y,␈↓πa␈↓↓,a(x,␈↓πa␈↓↓,␈↓πh␈↓↓)),␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ a(x,c(x,␈↓πh␈↓↓),␈↓πh␈↓↓) = ␈↓πh␈↓↓.␈↓
␈↓ ↓H␈↓[We␈α⊃note␈α⊃that␈α⊃the␈α⊃first␈α⊃argument␈α∩to␈α⊃the␈α⊃␈↓↓c␈↓␈α⊃and␈α⊃␈↓↓a␈↓␈α⊃functions␈α∩is␈α⊃a␈α⊃variable␈α⊃in␈α⊃the␈α⊃case␈α∩of␈α⊃the
␈↓ ↓H␈↓expression␈α
state␈α
vector␈α
while␈α
in␈α
the␈α
case␈α
of␈α
the␈α
machine␈α
state␈α
vector␈α
it␈α
is␈α
a␈α
register.␈α
This␈α
causes␈α
no
␈↓ ↓H␈↓difficulty,␈α⊃the␈α∩functions␈α⊃satisfy␈α∩the␈α⊃same␈α∩relations␈α⊃in␈α∩both␈α⊃cases␈α∩and␈α⊃can␈α∩just␈α⊃be␈α∩thought␈α⊃as
␈↓ ↓H␈↓polymorphic.]
␈↓ ↓H␈↓ Now we can define the semantics of the object language by
␈↓ ↓H␈↓↓␈↓ βxstep(x,␈↓πh␈↓↓) ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αif␈↓↓ isli(s) ␈↓αthen␈↓↓ a(ac,arg(s),␈↓πh␈↓↓)
␈↓ ↓H␈↓↓4.7)␈↓ ∧8␈↓αelse␈↓↓ ␈↓αif␈↓↓ isload(s) ␈↓αthen␈↓↓ a(ac,c(adr(s),␈↓πh␈↓↓),␈↓πh␈↓↓)
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ ␈↓αif␈↓↓ issto(s) ␈↓αthen␈↓↓ a(adr(s),c(ac,␈↓πh␈↓↓),␈↓πh␈↓↓)
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ ␈↓αif␈↓↓ isadd(s) ␈↓αthen␈↓↓ a(ac,c(adr(s),␈↓πh␈↓↓) + c(ac,␈↓πh␈↓↓),␈↓πh␈↓↓)
␈↓ ↓H␈↓which gives the state vector that results from executing an instruction and
␈↓ ↓H␈↓110␈↓ ¬wChapter VI␈↓ H
␈↓ ↓H␈↓4.8) ␈↓ β
␈↓↓outcome(p,␈↓πh␈↓↓) ← ␈↓αif␈↓↓ null(p) ␈↓αthen␈↓↓ ␈↓πh␈↓↓ ␈↓αelse␈↓↓ outcome(rest(p),step(first(p),␈↓πh␈↓↓))␈↓
␈↓ ↓H␈↓which gives the state vector that results from executing the program ␈↓↓p␈↓ with state vector ␈↓πh␈↓.
␈↓ ↓H␈↓ The following lemma is left as an exercise for the reader.
␈↓ ↓H␈↓4.9)␈↓ ∧_␈↓↓outcome(p1*p2,␈↓πh␈↓↓) = outcome(p2,outcome(p1,␈↓πh␈↓↓))␈↓
␈↓ ↓H␈↓5. ␈↓αThe compiler.␈↓
␈↓ ↓H␈↓ In␈α⊂order␈α⊃to␈α⊂compile␈α⊃an␈α⊂expression␈α⊂we␈α⊃need␈α⊂to␈α⊃know␈α⊂where␈α⊂the␈α⊃values␈α⊂of␈α⊃the␈α⊂variables
␈↓ ↓H␈↓occuring␈α⊃in␈α⊃the␈α⊂expression␈α⊃will␈α⊃be␈α⊃stored.␈α⊂ We␈α⊃shall␈α⊃assume␈α⊃that␈α⊂the␈α⊃function␈α⊃␈↓↓loc␈↓␈α⊃maps␈α⊂each
␈↓ ↓H␈↓variable␈α∩to␈α∩the␈α⊃register␈α∩where␈α∩it␈α⊃will␈α∩initially␈α∩be␈α⊃stored␈α∩in␈α∩the␈α⊃machine␈α∩state␈α∩vector.␈α∩ If␈α⊃the
␈↓ ↓H␈↓comparison␈αof␈αthe␈αinterpretation␈αof␈αan␈αexpression␈αand␈αthe␈αexecution␈αof␈αits␈αcompiled␈αcode␈αis␈αto␈αbe
␈↓ ↓H␈↓meaningful,␈αthe␈αinitial␈αstate␈αvectors␈αmust␈αagree␈αon␈αthe␈αvariables␈αoccuring␈αin␈αthe␈αexpression.␈α
Thus
␈↓ ↓H␈↓we will assume that the relation
␈↓ ↓H␈↓5.1)␈↓ ¬R␈↓↓c(loc(v),␈↓πh␈↓↓) = c(v,␈↓πx␈↓↓)␈↓
␈↓ ↓H␈↓between␈αthe␈αstate␈αvector␈α␈↓πh␈↓␈αbefore␈αthe␈αcompiled␈αprogram␈αstarts␈αto␈αact␈αand␈αthe␈αstate␈αvector␈α␈↓πx␈↓␈αof␈αthe
␈↓ ↓H␈↓source program holds.
␈↓ ↓H␈↓ Now we can write the compiler. It is
␈↓ ↓H␈↓↓␈↓ α8compile(e,t) ←
␈↓ ↓H␈↓↓␈↓ αx␈↓αif␈↓↓ isconst(e) ␈↓αthen␈↓↓ mkli(val(e))
␈↓ ↓H␈↓↓5.2)␈↓ αx␈↓αelse␈↓↓ ␈↓αif␈↓↓ isvar(e) ␈↓αthen␈↓↓ mkload(loc(e))
␈↓ ↓H␈↓↓␈↓ αx␈↓αelse␈↓↓ ␈↓αif␈↓↓ issum(e) ␈↓αthen␈↓↓ compile(s1(e),t) * mksto(t) * compile(s2(e),t + 1) * mkadd(t)
␈↓ ↓H␈↓Here␈α∞␈↓↓t␈↓␈α∞is␈α∞the␈α
number␈α∞of␈α∞a␈α∞register␈α
such␈α∞that␈α∞all␈α∞variables␈α
are␈α∞stored␈α∞in␈α∞registers␈α∞numbered␈α
less
␈↓ ↓H␈↓than ␈↓↓t,␈↓ so that registers ␈↓↓t␈↓ and above are available for temporary storage.
␈↓ ↓H␈↓ Before␈α
we␈α
can␈α
state␈α
our␈α
definition␈α
of␈αcorrectness␈α
of␈α
the␈α
compiler,␈α
we␈α
need␈α
a␈α
notion␈αof␈α
partial
␈↓ ↓H␈↓equality for state vectors
␈↓ ↓H␈↓␈↓ ε∪␈↓↓␈↓πz␈↓↓␈↓β1␈↓↓ =␈↓βA␈↓↓ ␈↓πz␈↓↓␈↓β2␈↓↓␈↓
␈↓ ↓H␈↓where␈α
␈↓πz␈↓␈↓β1␈↓␈α
and␈α
␈↓πz␈↓␈↓β2␈↓␈α
are␈α
state␈α
vectors␈α
and␈α
␈↓¬A␈α
␈↓is␈α
a␈α
set␈α
of␈α
variables␈α
means␈α
that␈αcorresponding␈α
components
␈↓ ↓H␈↓of␈α→␈↓πz␈↓␈↓β1␈↓␈α→and␈α_␈↓πz␈↓␈↓β2␈↓␈α→are␈α→equal␈α→except␈α_possibly␈α→for␈α→values,␈α→of␈α_variables␈α→in␈α→␈↓¬A.␈α→␈↓␈α_Symbolically,
␈↓ ↓H␈↓␈↓↓x ␈↓ππ␈↓↓ ␈↓¬A␈↓↓ ⊃ c(x,␈↓πz␈↓↓␈↓β1␈↓↓)=c(x,␈↓πz␈↓↓␈↓β2␈↓↓)␈↓. Partial equality satisfies the following relations:
␈↓ ↓H␈↓␈↓ ¬wChapter VI␈↓ ≠111
␈↓ ↓H␈↓5.3)␈↓ β8␈↓πz␈↓␈↓β1␈↓ = ␈↓πz␈↓␈↓β2␈↓ is equivalent to ␈↓πz␈↓␈↓β1␈↓ =␈↓β{ }␈↓ ␈↓πz␈↓␈↓β2␈↓ where { } denotes the empty set,
␈↓ ↓H␈↓5.4)␈↓ β8if ␈↓¬A ␈↓⊂ ␈↓¬B ␈↓and ␈↓πz␈↓␈↓β1␈↓ =␈↓βA␈↓ ␈↓πz␈↓␈↓β2␈↓ then ␈↓πz␈↓␈↓β1␈↓ =␈↓βB␈↓ ␈↓πz␈↓␈↓β2␈↓,
␈↓ ↓H␈↓5.5)␈↓ β8if ␈↓πz␈↓␈↓β1␈↓ =␈↓βA␈↓ ␈↓πz␈↓␈↓β2␈↓, then ␈↓↓a(x,␈↓πa␈↓↓,␈↓πz␈↓↓␈↓β1␈↓↓) =␈↓βA-{x}␈↓↓ a(x,␈↓πa␈↓↓,␈↓πz␈↓↓␈↓β2␈↓↓)␈↓,
␈↓ ↓H␈↓5.6)␈↓ β8if ␈↓↓x␈↓ ε ␈↓¬A ␈↓then ␈↓↓a(x,␈↓πa␈↓↓,␈↓πz␈↓↓) =␈↓βA␈↓↓ ␈↓πz␈↓↓␈↓,
␈↓ ↓H␈↓5.7)␈↓ β8if ␈↓πz␈↓␈↓β1␈↓ =␈↓βA␈↓ ␈↓πz␈↓␈↓β2␈↓ and ␈↓πz␈↓␈↓β2␈↓ =␈↓βB␈↓ ␈↓πz␈↓␈↓β3␈↓, then ␈↓πz␈↓␈↓β1␈↓ =␈↓βA∪B␈↓ ␈↓πz␈↓␈↓β3␈↓.
␈↓ ↓H␈↓In our case we need a specialization of this notation and will use
␈↓ ↓H␈↓␈↓ β8␈↓πz␈↓␈↓β1␈↓ =␈↓βt␈↓ ␈↓πz␈↓␈↓β2␈↓ to denote ␈↓πz␈↓␈↓β1␈↓ =␈↓β{x|x≥t}␈↓ ␈↓πz␈↓␈↓β2␈↓
␈↓ ↓H␈↓␈↓ β8␈↓πz␈↓␈↓β1␈↓ =␈↓βac␈↓ ␈↓πz␈↓␈↓β2␈↓ to denote ␈↓πz␈↓␈↓β1␈↓ =␈↓β{ac}␈↓ ␈↓πz␈↓␈↓β2␈↓ and
␈↓ ↓H␈↓␈↓ β8␈↓πz␈↓␈↓β1␈↓ =␈↓βt,ac␈↓ ␈↓πz␈↓␈↓β2␈↓ to denote a␈↓πz␈↓␈↓β1␈↓ =␈↓β{x|x = ac ∨ x ≥ t}␈↓ ␈↓πz␈↓␈↓β2␈↓.
␈↓ ↓H␈↓ The correctness of the compiler is stated in
␈↓ ↓H␈↓THEOREM␈α
1.␈α If␈α
␈↓πh␈↓␈α
and␈α␈↓πx␈↓␈α
are␈αmachine␈α
and␈α
source␈αlanguage␈α
state␈αvectors␈α
respectively␈α
such␈αthat
␈↓ ↓H␈↓(5.1) holds then
␈↓ ↓H␈↓␈↓ ∧(␈↓↓outcome(compile(e,t),␈↓πh␈↓↓) =␈↓βt␈↓↓ a(ac,value(e,␈↓πx␈↓↓),␈↓πh␈↓↓)␈↓
␈↓ ↓H␈↓It␈α∞states␈α∞that␈α∞the␈α∞result␈α∞of␈α∞running␈α∞the␈α∞compiled␈α∞program␈α∞is␈α∞to␈α∞put␈α∞the␈α∞value␈α∞of␈α∞the␈α∞expression
␈↓ ↓H␈↓compiled␈αinto␈αthe␈αaccumulator.␈α No␈αregisters␈αexcept␈αthe␈αaccumulator␈αand␈αthose␈αwith␈αaddresses␈α≥␈α␈↓↓t␈↓
␈↓ ↓H␈↓are affected.
␈↓ ↓H␈↓6. ␈↓αProof of Theorem 1.␈↓
␈↓ ↓H␈↓ The␈α
proof␈α
is␈α∞accomplished␈α
by␈α
an␈α∞induction␈α
on␈α
the␈α
expression␈α∞␈↓↓e␈↓␈α
being␈α
compiled␈α∞using␈α
the
␈↓ ↓H␈↓principle␈α
given␈α
in␈α
␈↓π∞␈↓3.␈α
We␈α
prove␈α
it␈α
first␈αfor␈α
constants,␈α
then␈α
for␈α
variables,␈α
and␈α
then␈α
for␈α
sums␈αon␈α
the
␈↓ ↓H␈↓induction hypothesis that it is true for the summands. Thus there are three cases.
␈↓ ↓H␈↓↓␈↓ βX␈↓αStatement␈↓ λxJustification␈↓↓
␈↓ ↓H␈↓↓␈↓I.␈↓↓ isconst(e).
␈↓ ↓H␈↓↓␈↓ ↓xoutcome(compile(e,t),␈↓πh␈↓↓)␈↓ ∧H= outcome(mkli(val(e)),␈↓πh␈↓↓)␈↓ _5.2
␈↓ ↓H␈↓↓␈↓ ∧H= step(mkli(val(e)),␈↓πh␈↓↓)␈↓ _4.8, 4.1
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,arg(mkli(val(e))),␈↓πh␈↓↓)␈↓ _4.1, 4.7
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,val(e),␈↓πh␈↓↓)␈↓ _4.1
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,value(e,␈↓πx␈↓↓),␈↓πh␈↓↓)␈↓ _1.1
␈↓ ↓H␈↓↓␈↓ ∧H=␈↓βt␈↓↓ a(ac,value(e,␈↓πx␈↓↓),␈↓πh␈↓↓).␈↓ _5.3, 5.4
␈↓ ↓H␈↓↓␈↓II.␈↓↓ isvar(e).
␈↓ ↓H␈↓112␈↓ ¬wChapter VI␈↓ H
␈↓ ↓H␈↓↓␈↓ ↓xoutcome(compile(e,t),␈↓πh␈↓↓)␈↓ ∧H= outcome(mkload(loc(e)),␈↓πh␈↓↓)␈↓ _5.2
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,c(adr(mkload(loc(e))),␈↓πh␈↓↓,␈↓πh␈↓↓)␈↓ _4.8, 4.2, 4.7
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,c(loc(e),␈↓πh␈↓↓),␈↓πh␈↓↓)␈↓ _4.2
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,c(e,␈↓πx␈↓↓),␈↓πh␈↓↓)␈↓ _5.1
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,value(e,␈↓πx␈↓↓),␈↓πh␈↓↓)␈↓ _1.1
␈↓ ↓H␈↓↓␈↓ ∧H=␈↓βt␈↓↓ a(ac,value(e,␈↓πx␈↓↓),␈↓πh␈↓↓).␈↓ _5.3, 5.4
␈↓ ↓H␈↓↓␈↓III.␈↓↓ issum(e).
␈↓ ↓H␈↓↓␈↓ ↓xoutcome(compile(e,t),␈↓πh␈↓↓)␈↓ ∧H= outcome(compile(s1(e),t) * mksto(t)␈↓ _5.2
␈↓ ↓H␈↓↓␈↓ ¬λ* compile(s2(e),t + 1) * mkadd(t),␈↓πh␈↓↓)
␈↓ ↓H␈↓↓6.1)␈↓ ∧H= outcome(mkadd(t),␈↓ _4.9
␈↓ ↓H␈↓↓␈↓ ¬λoutcome(compile(s2(e),t + 1),
␈↓ ↓H␈↓↓␈↓ ¬Houtcome(mksto(t),
␈↓ ↓H␈↓↓␈↓ ελoutcome(compile(s1(e),t),␈↓πh␈↓↓)))
␈↓ ↓H␈↓using␈α∞the␈α∞relation␈α∞between␈α∞concatenating␈α∞programs␈α∞and␈α∞composing␈α∞the␈α∞functions␈α∂they␈α∞represent.
␈↓ ↓H␈↓Now we introduce some notation. Let
␈↓ ↓H␈↓↓␈↓ ∧(v␈↓ ∧H= value(e,␈↓πx␈↓↓),
␈↓ ↓H␈↓↓6.2)␈↓ ∧(v␈↓β1␈↓↓␈↓ ∧H= value(s1(e),␈↓πx␈↓↓),
␈↓ ↓H␈↓↓␈↓ ∧(v␈↓β2␈↓↓␈↓ ∧H= value(s2(e),␈↓πx␈↓↓),
␈↓ ↓H␈↓so that ␈↓↓v = v␈↓β1␈↓↓ + v␈↓β2␈↓↓␈↓. Further let
␈↓ ↓H␈↓↓␈↓ ∧(␈↓πz␈↓↓␈↓β1␈↓↓␈↓ ∧H= outcome(compile(s1(e),t),␈↓πh␈↓↓),
␈↓ ↓H␈↓↓6.3)␈↓ ∧(␈↓πz␈↓↓␈↓β2␈↓↓␈↓ ∧H= outcome(mksto(t),␈↓πz␈↓↓␈↓β1␈↓↓),
␈↓ ↓H␈↓↓␈↓ ∧(␈↓πz␈↓↓␈↓β3␈↓↓␈↓ ∧H= outcome(compile(s2(e),t + 1),␈↓πz␈↓↓␈↓β2␈↓↓),
␈↓ ↓H␈↓↓␈↓ ∧(␈↓πz␈↓↓␈↓β4␈↓↓␈↓ ∧H= outcome(mkadd(t),␈↓πz␈↓↓␈↓β3␈↓↓)
␈↓ ↓H␈↓so that by (6.1) ␈↓↓␈↓πz␈↓↓␈↓β4␈↓↓ = outcome(compile(e,t),␈↓πh␈↓↓)␈↓, and the statement to be proved becomes
␈↓ ↓H␈↓↓␈↓ ∧(␈↓πz␈↓↓␈↓β4␈↓↓␈↓ ∧H=␈↓βt␈↓↓ a(ac,v,␈↓πh␈↓↓).
␈↓ ↓H␈↓In␈αorder␈αto␈αapply␈αthe␈αinduction␈αhypothesis␈αto␈αshow␈αthat␈α␈↓↓s2(e)␈↓␈αcompiles␈αcorrectly␈αwe␈αneed␈αto␈αknow
␈↓ ↓H␈↓that for each variable ␈↓↓v␈↓ the following equation holds:
␈↓ ↓H␈↓↓6.4)␈↓ β(c(loc(v),␈↓πz␈↓↓␈↓β2␈↓↓)␈↓ ∧H= c(v,␈↓πx␈↓↓)
␈↓ ↓H␈↓This is proved as follows:
␈↓ ↓H␈↓↓␈↓ β(c(loc(v),␈↓πz␈↓↓␈↓β2␈↓↓)␈↓ ∧H= c(loc(v),a(t,v␈↓β1␈↓↓,␈↓πh␈↓↓))␈↓ _␈↓since␈↓↓ loc(v) < t
␈↓ ↓H␈↓↓␈↓ ∧H= c(loc(v),␈↓πh␈↓↓)␈↓ _␈↓for the same reason␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧H= c(v,␈↓πx␈↓↓)␈↓ _5.1
␈↓ ↓H␈↓thus the induction hypothesis together with (5.1) and (6.4) and the definitions (6.3) give
␈↓ ↓H␈↓␈↓ ¬wChapter VI␈↓ ≠113
␈↓ ↓H␈↓↓6.5)␈↓ ∧(␈↓πz␈↓↓␈↓β1␈↓↓␈↓ ∧H= ␈↓βt␈↓↓ a(ac,v␈↓β1␈↓↓,␈↓πh␈↓↓)
␈↓ ↓H␈↓↓6.6)␈↓ ∧(␈↓πz␈↓↓␈↓β3␈↓↓␈↓ ∧H= ␈↓βt+1␈↓↓ a(ac,v␈↓β2␈↓↓,␈↓πz␈↓↓␈↓β2␈↓↓)
␈↓ ↓H␈↓Now we resume the main proof
␈↓ ↓H␈↓↓6.7)␈↓ βXc(ac,␈↓πz␈↓↓␈↓β1␈↓↓)␈↓ ∧H= v␈↓β1␈↓↓␈↓ _4.6, 6.5
␈↓ ↓H␈↓↓␈↓ ∧(␈↓πz␈↓↓␈↓β2␈↓↓␈↓ ∧H= outcome(mksto(t),␈↓πz␈↓↓␈↓β1␈↓↓)␈↓ _6.3
␈↓ ↓H␈↓↓␈↓ ∧H= a(t,c(ac,␈↓πz␈↓↓␈↓β1␈↓↓),␈↓πz␈↓↓␈↓β1␈↓↓)␈↓ _4.8, 4.3, 4.7
␈↓ ↓H␈↓↓␈↓ ∧H= a(t,v␈↓β1␈↓↓,␈↓πz␈↓↓␈↓β1␈↓↓)␈↓ _6.7
␈↓ ↓H␈↓↓␈↓ ∧H=␈↓βt+1␈↓↓ a(t,v␈↓β1␈↓↓,a(ac,v␈↓β1␈↓↓,␈↓πh␈↓↓))␈↓ _5.5, 6.5
␈↓ ↓H␈↓↓6.8)␈↓ ∧H=␈↓βt+1,ac␈↓↓ a(t,v␈↓β1␈↓↓,␈↓πh␈↓↓)␈↓ _5.6, 4.6
␈↓ ↓H␈↓↓6.9)␈↓ ∧(␈↓πz␈↓↓␈↓β3␈↓↓␈↓ ∧H= ␈↓βt+1␈↓↓ a(ac,v␈↓β2␈↓↓,a(t,v␈↓β1␈↓↓,␈↓πh␈↓↓))␈↓ _6.6, 4.6, 5.5, 6.8
␈↓ ↓H␈↓↓6.10)␈↓ βhc(ac,␈↓πz␈↓↓␈↓β3␈↓↓)␈↓ ∧H= v␈↓β2␈↓↓ ␈↓and␈↓↓ c(t,␈↓πz␈↓↓␈↓β3␈↓↓) = v␈↓β1␈↓↓␈↓ _4.6, 6.9
␈↓ ↓H␈↓↓␈↓ ∧(␈↓πz␈↓↓␈↓β4␈↓↓␈↓ ∧H= outcome(mkadd(t),␈↓πz␈↓↓␈↓β3␈↓↓)␈↓ _6.3
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,c(t,␈↓πz␈↓↓␈↓β3␈↓↓) + c(ac,␈↓πz␈↓↓␈↓β3␈↓↓),␈↓πz␈↓↓␈↓β3␈↓↓)␈↓ _4.8, 4.4, 4.7
␈↓ ↓H␈↓↓␈↓ ∧H= a(ac,v,␈↓πz␈↓↓␈↓β3␈↓↓)␈↓ _6.2, 6.10, 1.1
␈↓ ↓H␈↓↓␈↓ ∧H=␈↓βt+1␈↓↓ a(ac,v,a(ac,v␈↓β2␈↓↓,a(t,v␈↓β1␈↓↓,␈↓πh␈↓↓)))␈↓ _5.5, 6.9
␈↓ ↓H␈↓↓␈↓ ∧H=␈↓βt+1␈↓↓ a(ac,v,a(t,v␈↓β1␈↓↓,␈↓πh␈↓↓))␈↓ _4.6
␈↓ ↓H␈↓↓␈↓ ∧H=␈↓βt␈↓↓ a(ac,v,␈↓πh␈↓↓).␈↓ _4.6, 5.6, 5.7
␈↓ ↓H␈↓This concludes the proof.
␈↓ ↓H␈↓7. ␈↓αRemarks.␈↓
␈↓ ↓H␈↓ The␈α∩definition␈α∩of␈α∪correctness,␈α∩the␈α∩formalism␈α∩used␈α∪to␈α∩express␈α∩the␈α∩description␈α∪of␈α∩source
␈↓ ↓H␈↓language,␈α∞object␈α∞language␈α∞and␈α∞compiler,␈α∞and␈α∞the␈α∞methods␈α∞of␈α∞proof␈α∞are␈α∞all␈α∞intended␈α∞to␈α∂serve␈α∞as
␈↓ ↓H␈↓prototypes␈α⊃for␈α⊂the␈α⊃more␈α⊃complicted␈α⊂task␈α⊃of␈α⊂proving␈α⊃the␈α⊃correctness␈α⊂of␈α⊃usable␈α⊃compilers.␈α⊂ The
␈↓ ↓H␈↓ultimate␈α∂goal,␈α∂as␈α∂outlined␈α∂in␈α∂McCarthy␈α∂[1962a,1962b,1963,1964]␈α∞is␈α∂to␈α∂make␈α∂it␈α∂possible␈α∂to␈α∂use␈α∞a
␈↓ ↓H␈↓computer␈αto␈αcheck␈αproofs␈αthat␈αcompilers␈αare␈αcorrect.␈α The␈αconcepts␈αof␈αabstract␈αsyntax,␈αstate␈αvector,
␈↓ ↓H␈↓the␈α∪use␈α∀of␈α∪an␈α∀interpreter␈α∪for␈α∀defining␈α∪the␈α∪semantics␈α∀of␈α∪a␈α∀programming␈α∪language,␈α∀and␈α∪the
␈↓ ↓H␈↓definition␈α⊂of␈α⊂correctness␈α⊂of␈α⊂a␈α⊂compiler␈α⊂were␈α⊂introduced␈α⊂in␈α⊂McCarthy␈α⊂[1962b].␈α⊃ McCarthy␈α⊂and
␈↓ ↓H␈↓Painter, however, was the first in which the correctness of a compiler was proved.
␈↓ ↓H␈↓ The␈α∂problem␈α∂of␈α∞the␈α∂relations␈α∂between␈α∞source␈α∂language␈α∂and␈α∞object␈α∂language␈α∂arithmetic␈α∞is
␈↓ ↓H␈↓dealt␈αwith␈αhere␈αby␈αassuming␈αthat␈αthe␈α+␈αsigns␈αin␈αthe␈αdefinitions␈αof␈α␈↓↓value␈↓␈α(1.1)␈αand␈α␈↓↓step␈↓␈α(4.7)␈αwhich
␈↓ ↓H␈↓define␈αthe␈αsemantics␈α
of␈αthe␈αsource␈αand␈α
object␈αlanguages␈αrepresent␈α
the␈αsame␈αoperation.␈α Theorem␈α
1
␈↓ ↓H␈↓does not depend on any properties of this operation, not even commutativity or associativity.
␈↓ ↓H␈↓114␈↓ ¬wChapter VI␈↓ H
␈↓ ↓H␈↓ The␈α∪proof␈α∪is␈α∪entirely␈α∪straightforward␈α∪once␈α∪the␈α∪necessary␈α∪machinery␈α∪has␈α∀been␈α∪created.
␈↓ ↓H␈↓Additional␈α∂operations␈α∂such␈α∂as␈α∂subtraction,␈α⊂multiplication␈α∂and␈α∂division␈α∂could␈α∂be␈α⊂added␈α∂without
␈↓ ↓H␈↓essential␈α∞change␈α∞in␈α∞the␈α∞proof.␈α∞ For␈α∂example,␈α∞to␈α∞put␈α∞multiplication␈α∞into␈α∞the␈α∞system␈α∂the␈α∞following
␈↓ ↓H␈↓changes would be required.
␈↓ ↓H␈↓1.␈α∩ Use␈α⊃the␈α∩analytic␈α⊃syntax,␈α∩semantics,␈α⊃and␈α∩induction␈α⊃principle␈α∩given␈α⊃in␈α∩␈↓π∞␈↓1␈α⊃for␈α∩the␈α⊃language
␈↓ ↓H␈↓including products.
␈↓ ↓H␈↓2.␈α Add␈αan␈αinstruction␈α␈↓↓mul␈↓␈α␈↓↓x␈↓␈αand␈αthe␈αthree␈αsyntactical␈αfunctions␈α␈↓↓ismul(s),␈↓␈α␈↓↓adr(s),␈↓␈α␈↓↓mkmul(x)␈↓␈αto␈αthe
␈↓ ↓H␈↓abstract syntax of the object language together with the necessary relations among them.
␈↓ ↓H␈↓3. Add to the definition (4.7) of ␈↓↓step␈↓ the clause
␈↓ ↓H␈↓␈↓ ∧ε␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ismul(s) ␈↓αthen␈↓↓ a(ac,c(adr(s),␈↓πh␈↓↓) ␈↓π#␈↓↓ c(ac,␈↓πh␈↓↓),␈↓πh␈↓↓)␈↓.
␈↓ ↓H␈↓4. Add to the compiler (5.2) the clause
␈↓ ↓H␈↓␈↓ α4␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ isprod(e) ␈↓αthen␈↓↓ compile(p1(e),t) * mksto(t) * compile(p2(e),t + 1) * mkmul(t)␈↓.
␈↓ ↓H␈↓5. Add to the proof a case ␈↓↓isprod(e)␈↓ which parallels the case ␈↓↓issum(e)␈↓ exactly.
␈↓ ↓H␈↓Many extensions of this compiling problem are treated in Painter [1967].
␈↓ ↓H␈↓α␈↓ ε⊂Exercise
␈↓ ↓H␈↓1.␈α∂Modify␈α⊂the␈α∂arithmetic␈α∂expression␈α⊂language␈α∂to␈α⊂allow␈α∂variable␈α∂length␈α⊂sums.␈α∂ Describe␈α⊂how␈α∂to
␈↓ ↓H␈↓modify␈α
the␈αabstract␈α
syntax␈α
and␈αhow␈α
to␈α
fix␈αup␈α
the␈α
interpreter␈αand␈α
compiler,␈α
in␈αorder␈α
to␈α
give␈αthe
␈↓ ↓H␈↓semantics␈α
of␈α
the␈αnew␈α
language␈α
an␈α
compile␈αit.␈α
Are␈α
any␈α
modifications␈αof␈α
the␈α
object␈αlanguage␈α
syntax
␈↓ ↓H␈↓or␈αsemantics␈αneeded.␈α How␈αcan␈αthe␈αproof␈αof␈αcorrectness␈αof␈αthe␈αoriginal␈αcompiler␈αbe␈αturned␈αinto␈αa
␈↓ ↓H␈↓proof of correctness of this new compiler?
␈↓ ↓H␈↓2.␈α∃What␈α∃problems␈α∃are␈α∃encountered␈α∃when␈α∃conditional␈α∃expressions␈α∃are␈α∃added␈α∃to␈α⊗the␈α∃source
␈↓ ↓H␈↓language?␈α In␈αwhat␈αway␈αis␈αthe␈αcurrent␈αobject␈αlanguage␈αinadequate␈αfor␈αthis␈αcase?␈α How␈αwould␈αyou
␈↓ ↓H␈↓solve this problem?
␈↓ ↓H␈↓8. ␈↓αSome substantial exercises.␈↓
␈↓ ↓H␈↓ The␈α
following␈α
are␈α
some␈α
problems␈α
in␈α
programming␈α
and␈α
proving␈α
that␈α
can␈α
be␈α
solved␈α
using␈α
the
␈↓ ↓H␈↓techniques described in this chapter.
␈↓ ↓H␈↓1.␈α
Consider␈αthe␈α
class␈α
of␈αboolean␈α
conditional␈α
expressions␈α(bces).␈α
A␈αbce␈α
is␈α
either␈αa␈α
literal␈α
(␈↓↓lit)␈↓␈αor
␈↓ ↓H␈↓conditional␈α∞(␈↓↓if)␈↓␈α∞ composed␈α∞from␈α∞a␈α∞triple␈α∂of␈α∞bces␈α∞with␈α∞component␈α∞parts␈α∞␈↓↓premiss,␈↓␈α∂␈↓↓consequent␈↓␈α∞and
␈↓ ↓H␈↓␈↓↓alternate.␈↓␈α∞ Make␈α∂a␈α∞table␈α∞giving␈α∂the␈α∞abstract␈α∞syntax␈α∂of␈α∞bces.␈α∞ Define␈α∂an␈α∞evaluator␈α∞␈↓↓bval␈↓␈α∂for␈α∞bces
␈↓ ↓H␈↓given␈α∂that␈α∂␈↓↓lval␈↓␈α∂determines␈α∂the␈α∂truth␈α∂value␈α∂of␈α∂literals.␈α∂ Your␈α∂evaluator␈α∂should␈α∂satisfy␈α∂the␈α∂usual
␈↓ ↓H␈↓rules for conditional expressions as given in Chapter III (III.3.1) - (III.3.6).
␈↓ ↓H␈↓␈↓ ¬wChapter VI␈↓ ≠115
␈↓ ↓H␈↓ Work␈α
out␈αthe␈α
relations␈αbetween␈α
the␈αanalytic␈α
and␈αsynthetic␈α
syntaxes,␈αand␈α
give␈α
an␈αinduction
␈↓ ↓H␈↓principle for bces.
␈↓ ↓H␈↓ We␈α∂say␈α⊂that␈α∂a␈α⊂bce␈α∂is␈α∂in␈α⊂normal␈α∂form␈α⊂if␈α∂the␈α∂premiss␈α⊂of␈α∂every␈α⊂subexpression␈α∂is␈α⊂a␈α∂literal.
␈↓ ↓H␈↓Define a function that converts a bce to normal form.
␈↓ ↓H␈↓[Hint: Observe that in our external language the equivalence
␈↓ ↓H␈↓␈↓ αε␈↓↓␈↓αif␈↓↓ p ␈↓αthen␈↓↓ [␈↓αif␈↓↓ q ␈↓αthen␈↓↓ s ␈↓αelse␈↓↓ t] ␈↓αelse␈↓↓ [␈↓αif␈↓↓ r ␈↓αthen␈↓↓ s ␈↓αelse␈↓↓ t] ≡ ␈↓αif␈↓↓ [␈↓αif␈↓↓ p ␈↓αthen␈↓↓ q ␈↓αelse␈↓↓ r] ␈↓αthen␈↓↓ s ␈↓αelse␈↓↓ t␈↓
␈↓ ↓H␈↓holds.]
␈↓ ↓H␈↓Prove␈αthat␈αyour␈αfunction␈αis␈αtotal␈αand␈αthat␈αthe␈αresult␈αis␈αin␈αnormal␈αform.␈α Proving␈αtotality␈αwill␈αtake
␈↓ ↓H␈↓some␈α∂thought␈α∂to␈α∞get␈α∂the␈α∂correct␈α∂induction␈α∞predicate.␈α∂ Proving␈α∂correctness,␈α∞e.g.␈α∂that␈α∂the␈α∂result␈α∞is
␈↓ ↓H␈↓normal,␈αwill␈αrequire␈αmaking␈αa␈αformal␈αstatement␈αof␈αthe␈αnotion␈αof␈αnormality.␈α This␈αcan␈αbe␈αdone␈αby
␈↓ ↓H␈↓defining␈α∃a␈α∃predicate␈α⊗␈↓↓normal␈↓␈α∃on␈α∃bces.␈α∃ Show␈α⊗that␈α∃the␈α∃normalizing␈α∃transformation␈α⊗is␈α∃value
␈↓ ↓H␈↓preserving.
␈↓ ↓H␈↓2.␈α⊃Write␈α⊃the␈α⊃syntax␈α⊃for␈α⊃a␈α⊃language␈α⊃in␈α⊃which␈α⊃programs␈α⊃are␈α⊃sequences␈α⊃of␈α⊃statements␈α⊂(possibly
␈↓ ↓H␈↓labeled),␈αwhere␈αa␈αstatement␈αis␈αeither␈αan␈αassignment␈αof␈αa␈αvariable␈αto␈αthe␈αvalue␈αof␈αan␈αexpression,␈αa
␈↓ ↓H␈↓conditional␈αbranch␈αwhere␈αthe␈αonly␈αtests␈αare␈α␈↓↓e=0␈↓␈αand␈α␈↓↓e>0,␈↓␈αor␈αa␈αreturn␈αstatement␈αwhich␈αreturns␈αthe
␈↓ ↓H␈↓value␈α∩of␈α∩an␈α∩expression.␈α∩ For␈α∩simplicity␈α∩assume␈α⊃expressions␈α∩are␈α∩of␈α∩the␈α∩form␈α∩described␈α∩in␈α⊃␈↓π∞␈↓3.
␈↓ ↓H␈↓Extend␈αthe␈αmachine␈α
language␈αof␈α␈↓π∞␈↓4␈α
to␈αallow␈αconditional␈αbranches.␈α
Now␈αwrite␈αinterpreters␈α
and␈αa
␈↓ ↓H␈↓compiler␈α∪for␈α∪your␈α∪new␈α∪language␈α∪and␈α∪machine.␈α∪ Outline␈α∪a␈α∪proof␈α∪of␈α∪correctness.␈α∀ What␈α∪new
␈↓ ↓H␈↓difficulties are encountered when branches are allowed in the source language?
␈↓ ↓H␈↓116␈↓ εH␈↓ H
␈↓ ↓H␈↓α␈↓ ¬uChapter VII
␈↓ ↓H␈↓α␈↓ ¬/COMPILING IN LISP
␈↓ ↓H␈↓ Compiling␈αis␈αan␈αimportant␈αexample␈αof␈αsymbolic␈αcomputation␈αand␈αhas␈αreceived␈αmuch␈αstudy.
␈↓ ↓H␈↓Much␈α
of␈α
the␈α
study␈α
has␈α
been␈α
devoted␈α
to␈α
parsing␈α
which␈α
is␈α
essentially␈α
the␈α
transformation␈α
of␈αan␈α
input
␈↓ ↓H␈↓string␈α⊂in␈α⊂the␈α∂source␈α⊂language␈α⊂into␈α∂an␈α⊂internal␈α⊂form.␈α∂ The␈α⊂internal␈α⊂form␈α∂used␈α⊂depends␈α⊂on␈α∂the
␈↓ ↓H␈↓compiler.␈α⊂ Sometimes␈α⊂it␈α⊂is␈α⊂Polish␈α⊂prefix␈α⊂or␈α∂postfix␈α⊂notation,␈α⊂sometimes␈α⊂it␈α⊂is␈α⊂list␈α⊂structure,␈α∂and
␈↓ ↓H␈↓sometimes it consists of entries in a collection of tables.
␈↓ ↓H␈↓ When␈αinternal␈αnotation␈αLISP␈αis␈αbeing␈αcompiled,␈αthe␈αparsing␈αis␈αtrivial,␈αbecause␈αthe␈αinput␈αis
␈↓ ↓H␈↓S-expressions␈α
and␈αthe␈α
internal␈α
form␈αwanted␈α
is␈α
list␈αstructure,␈α
so␈αwhat␈α
parsing␈α
there␈αis␈α
is␈α
done␈αby
␈↓ ↓H␈↓the␈α∞ordinary␈α∞LISP␈α∞␈↓↓read␈↓␈α∂routine.␈α∞ Therefore,␈α∞compilers␈α∞can␈α∂be␈α∞very␈α∞compact␈α∞and␈α∂transparent␈α∞in
␈↓ ↓H␈↓structure,␈αand␈α
we␈αcan␈α
concentrate␈αour␈α
attention␈αon␈αthe␈α
code␈αgeneration␈α
phase.␈α This␈α
is␈αas␈αit␈α
should
␈↓ ↓H␈↓be,␈α∞because,␈α∞parsing␈α∞is␈α∞basically␈α∞a␈α∞side␈α∞issue␈α
in␈α∞compiling,␈α∞and␈α∞code␈α∞generation␈α∞is␈α∞the␈α∞matter␈α
of
␈↓ ↓H␈↓main scientific interest.
␈↓ ↓H␈↓ We␈α
shall␈αdescribe␈α
two␈α
compilers␈αin␈α
this␈αchapter␈α
called␈α
LCOM0␈αand␈α
LCOM4␈αwhich␈α
compile
␈↓ ↓H␈↓S-expression␈αLISP␈αinto␈αmachine␈α
language␈αfor␈αthe␈αPDP-10␈α
computer␈αaccording␈αto␈αthe␈α
conventions
␈↓ ↓H␈↓of␈α⊃MACLISP.␈α⊃ For␈α⊃now␈α∩we␈α⊃shall␈α⊃take␈α⊃these␈α∩conventions␈α⊃for␈α⊃granted.␈α⊃ Before␈α∩describing␈α⊃the
␈↓ ↓H␈↓compilers, we must describe these conventions.
␈↓ ↓H␈↓1. ␈↓αSome facts about the PDP-10.␈↓
␈↓ ↓H␈↓ The␈αtarget␈αlanguage␈αis␈αcalled␈αLAP␈αfor␈αLISP␈αassembly␈αprogram.␈α Each␈αfunction␈αis␈αcompiled
␈↓ ↓H␈↓separately␈αinto␈αa␈αseparate␈αLAP␈αprogram,␈αand␈αthese␈αprograms␈αare␈αwritten␈αonto␈αa␈αdisk␈αfile.␈α These
␈↓ ↓H␈↓files␈α∂can␈α∂later␈α∂be␈α∞read␈α∂into␈α∂a␈α∂LISP␈α∞core␈α∂image␈α∂and␈α∂assembled␈α∞in␈α∂place␈α∂by␈α∂the␈α∂LAP␈α∞assembler
␈↓ ↓H␈↓which␈αis␈αpart␈αof␈αthe␈αLISP␈αruntime␈αroutines.␈α
The␈αcompiler,␈αon␈αthe␈αother␈αhand,␈αis␈αa␈αseparate␈α
LISP
␈↓ ↓H␈↓core␈αimage␈αthat␈αcan␈αbe␈αinstructed␈αto␈αcompile␈αseveral␈αinput␈αfiles.␈α For␈αan␈αinput␈αfile␈αcalled␈α<name>,
␈↓ ↓H␈↓it␈α∞produces␈α∞an␈α∞output␈α
file␈α∞called␈α∞<name>.LAP.␈α∞ All␈α∞this␈α
is␈α∞specific␈α∞to␈α∞the␈α∞PDP-10␈α
time-sharing
␈↓ ↓H␈↓system␈αand␈αfortunately␈αworks␈αthe␈αsame␈αwhether␈αthe␈αtime-sharing␈αsystem␈αis␈αthe␈αD.E.C.␈αsystem,␈αthe
␈↓ ↓H␈↓Stanford system, or TENEX.
␈↓ ↓H␈↓ The␈αLAP␈αprogram␈αproduced␈αis␈αa␈αlist␈αof␈αwords;␈αthe␈αlast␈αword␈αis␈α␈↓¬NIL␈↓,␈αand␈αthe␈αfirst␈αword␈αis␈αa
␈↓ ↓H␈↓header␈α
of␈α
the␈α
form␈α
␈↓¬(LAP␈α␈↓↓fname␈↓¬␈α
SUBR)␈↓␈α
where␈α
␈↓↓fname␈↓␈α
is␈αthe␈α
name␈α
of␈α
the␈α
function␈α
compiled.␈α This
␈↓ ↓H␈↓header␈α
tells␈α
the␈α
DSKIN␈α
program␈α
that␈α
it␈α∞should␈α
read␈α
S-expressions␈α
till␈α
it␈α
comes␈α
to␈α
␈↓¬NIL␈↓␈α∞and␈α
then
␈↓ ↓H␈↓submit␈αwhat␈α
it␈αhas␈α
collected␈αto␈α
the␈αLAP␈α
assembler␈αwhich␈α
will␈αassemble␈α
it␈αinto␈α
core.␈α The␈α
rest␈αof
␈↓ ↓H␈↓the words are either atoms representing labels or lists representing single PDP-10 instructions.
␈↓ ↓H␈↓ The following facts about the PDP-10 may be of use:
␈↓ ↓H␈↓ The␈αPDP-10␈αhas␈αa␈α36␈αbit␈αword␈αand␈α
an␈α18␈αbit␈αaddress.␈α In␈αinstructions␈αand␈αin␈α
accumulators
␈↓ ↓H␈↓used␈α∩as␈α⊃index␈α∩registers␈α⊃the␈α∩address␈α∩is␈α⊃found␈α∩in␈α⊃the␈α∩right␈α∩part␈α⊃of␈α∩the␈α⊃word␈α∩where␈α∩the␈α⊃least
␈↓ ↓H␈↓significant bits in arithmetic reside.
␈↓ ↓H␈↓ There␈α
are␈α
16␈α
general␈α
registers␈α
which␈α
serve␈α
simultaneously␈α
as␈α
accumulators␈α
(receiving␈αthe
␈↓ ↓H␈↓␈↓ ¬qChapter VII␈↓ ≠117
␈↓ ↓H␈↓results␈αof␈αarithmetic␈αoperations),␈αindex␈αregisters␈α(modifying␈αthe␈αnominal␈αaddresses␈αof␈α instructions
␈↓ ↓H␈↓to␈α
form␈α
effective␈α
addresses),␈α
and␈α
as␈α
the␈α
first␈α
16␈α
registers␈α
of␈α
memory␈α
(if␈α
the␈α
effective␈α
address␈α
of␈α
an
␈↓ ↓H␈↓instruction␈α
is␈α less␈α
than␈α 16,␈α
then␈α the␈α
instruction␈αuses␈α
the␈αcorresponding␈α
general␈αregister␈α
as␈αits
␈↓ ↓H␈↓operand.)
␈↓ ↓H␈↓ All␈α
instructions␈α
have␈α
the␈α
same␈α
format␈α
and␈α
are␈α
written␈α
for␈α
the␈α
LAP␈α
assembly␈α
program␈α
in␈α
the
␈↓ ↓H␈↓form
␈↓ ↓H␈↓ (<op name> <accumulator> <address> <index register>).
␈↓ ↓H␈↓Thus␈α ␈↓¬(MOVE␈α1␈α
3␈αP)␈↓␈α causes␈αaccumulator 1␈α
to␈αreceive␈αthe␈αcontents␈α
of␈αa␈α memory␈α
register␈αwhose
␈↓ ↓H␈↓address␈α
is 3+c(P),␈α
i.e.␈α
3+<the␈α
contents␈α
of␈α
general␈αregister␈α
P>.␈α
<address>␈α
may␈α
be␈α
a␈α
number,␈αa␈α
label,
␈↓ ↓H␈↓or␈α
an␈α
S-expression␈α
constant␈αin␈α
the␈α
form␈α
␈↓¬(QUOTE␈α␈↓↓e␈↓¬)␈↓␈α
where␈α
␈↓↓e␈↓␈α
is␈αan␈α
S-expression.␈α
The␈α
latter␈αallows␈α
a
␈↓ ↓H␈↓quoted␈α⊃S-expression␈α⊃to␈α⊃be␈α⊃loaded␈α⊃into␈α⊃accumulator 1␈α⊃by␈α⊃the␈α⊃instruction␈α⊃ ␈↓¬(MOVEI 1 (QUOTE ␈↓↓e␈↓¬))␈↓.
␈↓ ↓H␈↓An␈αaddtional␈α
form␈αthat␈α
appears␈αin␈α
the␈αaddress␈α
field␈αfor␈α
some␈αinstructions␈α
is␈α␈↓¬(% 0 0 ␈↓↓m␈↓¬␈α
␈↓↓m␈↓¬)␈↓␈α.␈α This␈α
is
␈↓ ↓H␈↓a␈α
literal␈αwith␈α
value␈α
a␈αword␈α
containing␈α␈↓↓m␈↓␈α
in␈α
the␈αlefthand␈α
side␈αand␈α
␈↓↓n␈↓␈α
in␈αthe␈α
righthand␈αside.␈α
Which
␈↓ ↓H␈↓form of <address> is meaningful depends on the instruction.
␈↓ ↓H␈↓ To␈α
determine␈α
the␈αeffective␈α
address␈α
of␈αan␈α
instruction␈α
the␈αfollowing␈α
rules␈α
apply.␈α
The␈αvalue
␈↓ ↓H␈↓of␈α<address>␈α
is␈αcombined␈αwith␈α
the␈αcontents␈αof␈α
the␈αindex␈αregister␈α
to␈αform␈αa␈α
direct␈αaddress.␈α
If␈αno
␈↓ ↓H␈↓index␈αregister␈αis␈αspecified,␈αthen␈αjust␈αthe␈αvalue␈αof␈α<address>␈αis␈αused.␈α If␈αan␈α@␈αoccurs␈αin␈αthe␈αlist␈αas␈α
a
␈↓ ↓H␈↓separate␈αsymbol,␈α then␈αthe␈αmemory␈αreference␈αis␈αindirect,␈αi.e.␈αthe␈αeffective␈αaddress␈αis␈αthe␈αcontents␈α
of
␈↓ ↓H␈↓the␈α⊂right␈α⊂half␈α⊃of␈α⊂the␈α⊂word␈α⊃ directly␈α⊂ addressed␈α⊂ by␈α⊂ the␈α⊃ instruction␈α⊂(modified␈α⊂ by␈α⊃ the␈α⊂index
␈↓ ↓H␈↓register and indirect bit of that word).
␈↓ ↓H␈↓ In␈α∞the␈α∞following␈α∞description␈α
of␈α∞some␈α∞instructions␈α∞useful␈α
in␈α∞constructing␈α∞the␈α∞compiler,␈α
<ef>
␈↓ ↓H␈↓denotes the effective address of an instruction and ac denotes the accumulator.
␈↓ ↓H␈↓␈↓ αHMOVE ␈↓ ¬(c(ac) ← c(<ef>)
␈↓ ↓H␈↓␈↓ αHMOVEI ␈↓ ¬(c(ac) ← <ef>
␈↓ ↓H␈↓␈↓ αHMOVEM ␈↓ ¬(c(<ef>) ← c(ac)
␈↓ ↓H␈↓␈↓ αHHLRZ ␈↓ ¬(right half of c(ac) ← left half of c(<ef>)
␈↓ ↓H␈↓␈↓ αHHRRZ ␈↓ ¬(right half of c(ac) ← right half of c(<ef>)
␈↓ ↓H␈↓␈↓ αH␈↓ β8(These two are used indirectly for car and cdr respectively.)
␈↓ ↓H␈↓␈↓ αHADD ␈↓ ¬(c(ac) ← c(ac) + c(<ef>)
␈↓ ↓H␈↓␈↓ αHSUB ␈↓ ¬(c(ac) ← c(ac) - c(<ef>)
␈↓ ↓H␈↓␈↓ αHJRST ␈↓ ¬(go to <ef>
␈↓ ↓H␈↓␈↓ αHJUMPE ␈↓ ¬(if c(ac) = 0 then go to <ef>
␈↓ ↓H␈↓␈↓ αHJUMPN ␈↓ ¬(if c(ac) ≠ 0 then go to <ef>
␈↓ ↓H␈↓␈↓ αHCAME ␈↓ ¬(if c(ac) = c(<ef>) then <skip next instruction>
␈↓ ↓H␈↓␈↓ αHCAMN ␈↓ ¬(if c(ac) ≠ c(<ef>) then <skip next instruction>
␈↓ ↓H␈↓␈↓ αHPUSH ␈↓ ¬(c(c(right␈αhalf␈αof␈αac))␈α←␈αc(<ef>);␈αthe␈αcontents␈αof␈αeach
␈↓ ↓H␈↓␈↓ ¬(half␈α
of␈αac␈α
is␈α
increased␈αby␈α
one␈α
and␈αif␈α
the␈αcontents␈α
of
␈↓ ↓H␈↓␈↓ ¬(the␈α∞left␈α∞half␈α∞is␈α∞then␈α∞ 0,␈α∞a␈α∞stack␈α∞overflow␈α∞interrupt
␈↓ ↓H␈↓␈↓ ¬(occurs.␈α
(PUSH␈α
P␈α
ac)␈α
is␈α
used␈α
in␈α
LISP␈α
to␈α
put␈α
c(ac)
␈↓ ↓H␈↓␈↓ ¬(on the stack.
␈↓ ↓H␈↓␈↓ αHPOP ␈↓ ¬(c(<ef>)␈α
←c(c(right␈α∞half␈α
of␈α
ac));␈α∞the␈α
contents␈α∞of␈α
each
␈↓ ↓H␈↓␈↓ ¬(half␈α∞of␈α∞ac␈α∞are␈α∞then␈α∞decreased␈α∞by␈α∞1.␈α∞ This␈α∂may␈α∞be
␈↓ ↓H␈↓␈↓ ¬(used␈α∩for␈α⊃removing␈α∩the␈α∩top␈α⊃element␈α∩of␈α∩the␈α⊃stack.
␈↓ ↓H␈↓␈↓ ¬(Thus␈α(POP␈αP␈α
1)␈αputs␈αthe␈α
top␈αelement␈αof␈α
the␈αstack
␈↓ ↓H␈↓118␈↓ ¬qChapter VII␈↓ H
␈↓ ↓H␈↓␈↓ ¬(in␈αaccumulator␈α1.␈α The␈αi-th␈αelement␈αof␈αthe␈α
stack␈αis
␈↓ ↓H␈↓␈↓ ¬(obtained by (MOVE 1 @ i P).
␈↓ ↓H␈↓␈↓ αHPOPJ ␈↓ ¬((POPJ P) is used for returning from a subroutine
␈↓ ↓H␈↓ These␈α∞instructions␈α∞are␈α∞adequate␈α∞for␈α∞compiling␈α∂basic␈α∞LISP␈α∞code␈α∞with␈α∞ the␈α∞addition␈α∂of␈α∞the
␈↓ ↓H␈↓subroutine␈αcalling␈αpseudo-instrucion.␈α The␈αform␈αof␈αthis␈αinstruction␈αis␈α␈↓¬(CALL ␈↓↓n␈↓¬␈α(QUOTE ␈↓<subr>␈↓¬))␈↓␈α.␈αIt
␈↓ ↓H␈↓is␈αused␈αfor␈αcalling␈αthe␈αLISP␈αsubroutine␈α<subr>␈αwith␈α ␈↓↓n␈↓␈αarguments.␈α The␈α convention␈α is␈αthat␈αthe
␈↓ ↓H␈↓arguments␈αwill␈αbe␈αstored␈αin␈αsuccessive␈αaccumulators␈αbeginning␈αwith␈αaccumulator␈α 1,␈αand␈αthe␈αresult
␈↓ ↓H␈↓will␈α be␈α returned␈α in␈α accumulator 1.␈α In␈αparticular␈αthe␈αfunctions␈α ␈↓¬ATOM␈α␈↓␈αand␈α ␈↓¬CONS␈α␈↓␈αare␈αcalled
␈↓ ↓H␈↓with␈α
␈↓¬(CALL 1 (QUOTE ATOM))␈↓␈α
and␈α
␈↓¬(CALL 2 (QUOTE CONS))␈↓␈α
respectively.␈α
Programs␈α∞produced␈α
by
␈↓ ↓H␈↓you␈α
will␈αbe␈α
called␈αsimilarly␈α
when␈αtheir␈α
names␈αare␈α
referred␈αto.␈α
The␈αdetails␈α
of␈αhow␈α
␈↓¬CALL␈α␈↓works␈α
are
␈↓ ↓H␈↓unimportant␈α∪here;␈α∪it␈α∪actually␈α∪traps␈α∪to␈α∪a␈α∪machine␈α∪language␈α∪routine␈α∪that␈α∪checks␈α∪whether␈α∩the
␈↓ ↓H␈↓function␈αis␈αbeing␈αtraced␈αand␈αwhich␈αcan␈αreplace␈αthe␈α␈↓¬CALL␈α␈↓by␈α␈↓¬PUSHJ␈α␈↓for␈αgreater␈αspeed␈αwhen␈αtracing
␈↓ ↓H␈↓is definitely not wanted.
␈↓ ↓H␈↓ There␈α
are␈α
minor␈α
modifications␈αin␈α
the␈α
form␈α
of␈α
some␈αof␈α
the␈α
instructions␈α
for␈α
compiling␈αcode
␈↓ ↓H␈↓for␈α⊂the␈α∂LISP1.6␈α⊂assembler.␈α⊂ These␈α∂include␈α⊂the␈α∂calling␈α⊂instruction␈α⊂which␈α∂has␈α⊂the␈α⊂form␈α∂␈↓¬(CALL ␈↓↓n␈↓¬
␈↓ ↓H␈↓¬(QUOTE ␈↓<subr>␈↓¬) S)␈↓␈α⊃,␈α⊃the␈α∩form␈α⊃of␈α⊃the␈α⊃special␈α∩literal␈α⊃which␈α⊃is␈α⊃␈↓¬(C ␈↓↓m␈↓¬␈α∩0 ␈↓↓n␈↓¬␈α⊃0)␈↓␈α⊃and␈α⊃the␈α∩manner␈α⊃of
␈↓ ↓H␈↓indicating␈α∞indirect␈α∞addressing,␈α∞which␈α∞is␈α∞to␈α∞attach␈α∞the␈α∞@␈α∞sign␈α∞to␈α∞the␈α∞operation␈α∞name␈α∞rather␈α
than
␈↓ ↓H␈↓putting in as a separate element of the list.
␈↓ ↓H␈↓2. ␈↓αCode produced by LISP compilers.␈↓
␈↓ ↓H␈↓ We␈αwill␈αdiscuss␈α
two␈αcompilers,␈αa␈αsimple␈α
one␈αcalled␈αLCOM0␈α
and␈αa␈αmore␈αoptimising␈α
compiler
␈↓ ↓H␈↓called␈αLCOM4.␈α LCOM4␈αproduces␈α
about␈αhalf␈αas␈αmany␈αinstructions␈α
for␈αa␈αgiven␈αfunction␈α
as␈αdoes
␈↓ ↓H␈↓LCOM0.␈α⊃Besides␈α⊃these,␈α⊂there␈α⊃are␈α⊃the␈α⊂standard␈α⊃PDP-10␈α⊃LISP␈α⊂compiler␈α⊃written␈α⊃at␈α⊃M.I.T.␈α⊂ by
␈↓ ↓H␈↓Richard␈α⊂Greenblatt␈α⊂and␈α⊂Stuart␈α⊂Nelson␈α⊃and␈α⊂modified␈α⊂by␈α⊂Whitfield␈α⊂Diffie␈α⊂and␈α⊃the␈α⊂MACLISP
␈↓ ↓H␈↓compiler NCOMPLR.
␈↓ ↓H␈↓ At␈α
the␈α
end␈α
of␈α
this␈α
section␈α
are␈α
examples␈α
of␈α
the␈α
output␈α
of␈α
each␈α
of␈α
the␈α
compilers␈α
mentioned
␈↓ ↓H␈↓above for the file containing:
␈↓ ↓H␈↓ ␈↓¬(DEFPROP DROP␈↓
␈↓ ↓H␈↓ ␈↓¬(LAMBDA(U)␈↓
␈↓ ↓H␈↓ ␈↓¬(COND ((NULL U) NIL) (T (CONS (LIST (CAR U)) (DROP (CDR U))))))␈↓
␈↓ ↓H␈↓ ␈↓¬EXPR)␈↓
␈↓ ↓H␈↓ The following comments are with regard to these examples.
␈↓ ↓H␈↓ 1.␈α
Note␈α
that␈α
all␈α
four␈α
compilers␈α
produce␈αthe␈α
same␈α
first␈α
line␈α
of␈α
heading.␈α
This␈α
is␈αnecessary␈α
for
␈↓ ↓H␈↓the␈α⊃LAP␈α⊃assembly␈α⊂program␈α⊃ which␈α⊃ also␈α⊃requires␈α⊂ the␈α⊃ ␈↓¬NIL␈↓ ␈α⊃at␈α⊂the␈α⊃end␈α⊃as␈α⊃punctuation.␈α⊂ The
␈↓ ↓H␈↓standard␈αcompiler␈α
and␈αNCOMPLR␈αuse␈α
a␈αfunction␈αcalled␈α
␈↓¬XCONS ␈α␈↓that␈αhas␈α
the␈α same␈α
effect␈α as
␈↓ ↓H␈↓␈↓¬CONS␈α␈↓␈αexcept␈α that␈α
it␈αreceives␈αits␈αarguments␈αin␈α
the␈αreverse␈αorder␈αwhich␈αis␈α
sometimes␈αconvenient.
␈↓ ↓H␈↓This␈αrequires,␈αof␈αcourse,␈αthat␈αthe␈αcompiler␈αbe␈αsmart␈αenough␈αto␈αdecide␈αwhere␈αit␈αis␈αmore␈αconvenient
␈↓ ↓H␈↓to␈α
put␈α
the␈α
arguments␈α
of␈α
the␈α␈↓↓cons␈↓␈α
function.␈α
They␈α
also␈α
use␈α
␈↓¬NCONS␈α
␈↓rather␈αthan␈α
␈↓¬LIST␈α
␈↓␈α
to␈α
form␈α
a␈αlist␈α
of
␈↓ ↓H␈↓one element.
␈↓ ↓H␈↓␈↓ ¬qChapter VII␈↓ ≠119
␈↓ ↓H␈↓ 2.␈α∂The␈α⊂two␈α∂compilers,␈α⊂LCOM0␈α∂and␈α⊂LCOM4,␈α∂are␈α⊂similar␈α∂in␈α⊂structure,␈α∂but␈α⊂LCOM4,␈α∂the
␈↓ ↓H␈↓better one, which is twice as long, uses the following optimisations.
␈↓ ↓H␈↓ 2.1.␈α∂When␈α⊂the␈α∂argument␈α⊂of␈α∂␈↓¬CAR␈α⊂␈↓or␈α∂␈↓¬CDR␈α∂␈↓is␈α⊂a␈α∂variable,␈α⊂it␈α∂compiles␈α⊂a␈α∂ ␈↓¬(HLRZ 1 @ ␈↓↓i␈↓¬␈α⊂P)␈↓␈α∂ or
␈↓ ↓H␈↓␈↓¬(HRRZ 1 @ ␈↓↓i␈↓¬␈αP)␈↓␈α which␈αgets␈αthe␈αresult␈αthrough␈αthe␈αstack␈αwithout␈αfirst␈αcompiling␈αthe␈αargument␈αinto
␈↓ ↓H␈↓an accumulator.
␈↓ ↓H␈↓ 2.2.␈α
When␈α
it␈α
has␈α
to␈α
set␈α
up␈α
the␈α
arguments␈α
of␈α
a␈α
function␈α
in␈α
the␈α
accumulators,␈α
in␈αgeneral,␈α
the
␈↓ ↓H␈↓program␈α
must␈α
compute␈α
the␈α
arguments␈α
one␈α
at␈α
a␈αtime␈α
and␈α
save␈α
them␈α
on␈α
the␈α
stack,␈α
and␈α
then␈αload
␈↓ ↓H␈↓the␈α∞ accumulators␈α∞from␈α∞the␈α∞stack.␈α∞ However,␈α∞if␈α∂one␈α∞of␈α∞the␈α∞arguments␈α∞is␈α∞a␈α∞variable,␈α∞is␈α∂a␈α∞quoted
␈↓ ↓H␈↓expression,␈αor␈αcan␈αbe␈αobtained␈αfrom␈αa␈αvariable␈αby␈αa␈α chain␈α of␈α ␈↓¬CAR␈↓s ␈α and␈α ␈↓¬CDR␈↓s,␈α then␈α it␈α need
␈↓ ↓H␈↓not␈αbe␈αcomputed␈α
until␈αthe␈αtime␈α
of␈αloading␈α accumulators␈α
since␈α it␈α can␈α
be␈α computed␈α using␈α
only
␈↓ ↓H␈↓the accumulator in which it is wanted.
␈↓ ↓H␈↓ 3.␈α⊂ The␈α⊂ Diffie␈α⊂ compiler␈α⊂ produces␈α⊂about␈α⊂10␈α⊂percent␈α⊂less␈α⊂code␈α⊂than␈α⊂LCOM4;␈α⊂the␈α∂main
␈↓ ↓H␈↓difference␈α∞seems␈α∞to␈α∂be␈α∞that␈α∞it␈α∂sometimes␈α∞notices␈α∞when␈α∂ it␈α∞ has␈α∞ an␈α∂ expression␈α∞that␈α∞it␈α∂will␈α∞need
␈↓ ↓H␈↓later.␈αSometimes␈α
this␈αfeature␈αleads␈α
it␈αastray.␈α
For␈αexample,␈αit␈α
may␈αsave␈α
␈↓αa|␈↓␈↓↓u␈↓␈α ␈αfor␈α
later␈αuse␈αat␈α
greater
␈↓ ↓H␈↓cost than re-computing it.
␈↓ ↓H␈↓ 4.␈α∩ NCOMPLR␈α∩is␈α∩noted␈α∩particularly␈α∩for␈α∩its␈α∩efficient␈α∩code␈α∩for␈α∩numerical␈α∩computations.
␈↓ ↓H␈↓Hence␈α_the␈α_"N".␈α_ Except␈α_for␈α→some␈α_bookkeeping␈α_code␈α_initially,␈α_the␈α_Diffie␈α→compiler␈α_and
␈↓ ↓H␈↓NCOMPLR produce similar code for this example.
␈↓ ↓H␈↓120␈↓ ¬qChapter VII␈↓ H
␈↓ ↓H␈↓******************************************************************
␈↓ ↓H␈↓LCOM0 produces the following code for the MACLISP LAP assembler.
␈↓ ↓H␈↓¬(LAP DROP SUBR)
␈↓ ↓H␈↓¬(PUSH P 1)
␈↓ ↓H␈↓¬(MOVE 1 0 P)
␈↓ ↓H␈↓¬(PUSH P 1)
␈↓ ↓H␈↓¬(MOVE 1 0 P)
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1))
␈↓ ↓H␈↓¬(CALL 1 (QUOTE NULL))
␈↓ ↓H␈↓¬(JUMPE 1 G0003)
␈↓ ↓H␈↓¬(MOVEI 1 0)
␈↓ ↓H␈↓¬(JRST 0 G0002)
␈↓ ↓H␈↓¬G0003
␈↓ ↓H␈↓¬(MOVEI 1 (QUOTE T))
␈↓ ↓H␈↓¬(JUMPE 1 G0004)
␈↓ ↓H␈↓¬(MOVE 1 0 P)
␈↓ ↓H␈↓¬(PUSH P 1)
␈↓ ↓H␈↓¬(MOVE 1 0 P)
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1))
␈↓ ↓H␈↓¬(CALL 1 (QUOTE CAR))
␈↓ ↓H␈↓¬(PUSH P 1)
␈↓ ↓H␈↓¬(MOVE 1 0 P)
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1))
␈↓ ↓H␈↓¬(CALL 1 (QUOTE LIST))
␈↓ ↓H␈↓¬(PUSH P 1)
␈↓ ↓H␈↓¬(MOVE 1 -1 P)
␈↓ ↓H␈↓¬(PUSH P 1)
␈↓ ↓H␈↓¬(MOVE 1 0 P)
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1))
␈↓ ↓H␈↓¬(CALL 1 (QUOTE CDR))
␈↓ ↓H␈↓¬(PUSH P 1)
␈↓ ↓H␈↓¬(MOVE 1 0 P)
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1))
␈↓ ↓H␈↓¬(CALL 1 (QUOTE DROP))
␈↓ ↓H␈↓¬(PUSH P 1)
␈↓ ↓H␈↓¬(MOVE 1 -1 P)
␈↓ ↓H␈↓¬(MOVE 2 0 P)
␈↓ ↓H␈↓¬(SUB P (% 0 0 2 2))
␈↓ ↓H␈↓¬(CALL 2 (QUOTE CONS))
␈↓ ↓H␈↓¬(JRST 0 G0002)
␈↓ ↓H␈↓¬G0004
␈↓ ↓H␈↓¬G0002
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1))
␈↓ ↓H␈↓¬(POPJ P)
␈↓ ↓H␈↓¬NIL
␈↓ ↓H␈↓␈↓ ¬qChapter VII␈↓ ≠121
␈↓ ↓H␈↓******************************************************************
␈↓ ↓H␈↓LCOM4 produces the following code for the MACLISP LAP assembler.
␈↓ ↓H␈↓¬(LAP DROP SUBR)
␈↓ ↓H␈↓¬(PUSH P 1) ~save argument, U, on stack
␈↓ ↓H␈↓¬(MOVE 1 0 P) ~get U off stack
␈↓ ↓H␈↓¬(JUMPE 1 G0003) ~if U=NIL then exit
␈↓ ↓H␈↓¬(HLRZ 1 @ 0 P) ~car U
␈↓ ↓H␈↓¬(CALL 1 (QUOTE LIST)) ~<car U>
␈↓ ↓H␈↓¬(PUSH P 1) ~save <car U>
␈↓ ↓H␈↓¬(HRRZ @ 1 -1 P) ~cdr U (U now 1 below top of stack
␈↓ ↓H␈↓¬(CALL 1 (QUOTE DROP)) ~drop cdr U
␈↓ ↓H␈↓¬(MOVE 2 1) ~set up arguments for cons
␈↓ ↓H␈↓¬(MOVE 1 0 P)
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1)) ~reset stack to arglist
␈↓ ↓H␈↓¬(CALL 2 (QUOTE CONS)) ~cons[<car u>,cdr U]
␈↓ ↓H␈↓¬G0003
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1)) ~reset stack to position at entry
␈↓ ↓H␈↓¬(POPJ P) ~return
␈↓ ↓H␈↓¬NIL
␈↓ ↓H␈↓******************************************************************
␈↓ ↓H␈↓The Diffie compiler produces the following code for the LISP1.6 assembler.
␈↓ ↓H␈↓¬(LAP DROP SUBR)
␈↓ ↓H␈↓¬ (PUSH P 1)
␈↓ ↓H␈↓¬ (JUMPE 1 TAG1)
␈↓ ↓H␈↓¬ (HLRZ@ 1 0 P)
␈↓ ↓H␈↓¬ (CALL 1 (E NCONS) S)
␈↓ ↓H␈↓¬ (PUSH P 1)
␈↓ ↓H␈↓¬ (HRRZ@ 1 -1 P)
␈↓ ↓H␈↓¬ (CALL 1 (E DROP) S)
␈↓ ↓H␈↓¬ (POP P 2)
␈↓ ↓H␈↓¬ (CALL 2 (E XCONS) S)
␈↓ ↓H␈↓¬ TAG1 (SUB P (C O 0 1 1))
␈↓ ↓H␈↓¬ (POPJ P)
␈↓ ↓H␈↓¬ NIL
␈↓ ↓H␈↓122␈↓ ¬qChapter VII␈↓ H
␈↓ ↓H␈↓******************************************************************
␈↓ ↓H␈↓The MACLISP compiler, NCOMPLR, produces the following code:
␈↓ ↓H␈↓ (The␈α∞pseudo-instruction␈α∞ARGS␈α∞tells␈α∞the␈α∞assembler␈α∞the␈α∞number␈α∞of␈α∞arguments␈α∂expected␈α∞by
␈↓ ↓H␈↓the␈α∞function.␈α∂ The␈α∞instruction␈α∂JSP␈α∞is␈α∂a␈α∞special␈α∂fast␈α∞function␈α∂call␈α∞done␈α∂by␈α∞jumping␈α∂rather␈α∞than
␈↓ ↓H␈↓going␈α⊃through␈α⊃the␈α⊃regular␈α⊃calling␈α∩mechanism.␈α⊃ T␈α⊃denotes␈α⊃an␈α⊃accumulator␈α⊃designated␈α∩to␈α⊃hold
␈↓ ↓H␈↓temporary values.)
␈↓ ↓H␈↓¬(LAP DROP SUBR)
␈↓ ↓H␈↓¬(ARGS DROP (NIL . 1))
␈↓ ↓H␈↓¬(PUSH P 1)
␈↓ ↓H␈↓¬(JSP T PDLNMK)
␈↓ ↓H␈↓¬(JUMPE 1 G0001)
␈↓ ↓H␈↓¬(HLRZ 1 @ 0 P)
␈↓ ↓H␈↓¬(JSP T %NCONS)
␈↓ ↓H␈↓¬(PUSH P 1)
␈↓ ↓H␈↓¬(HRRZ 1 @ -1 P)
␈↓ ↓H␈↓¬(CALL 1 'DROP)
␈↓ ↓H␈↓¬(POP P 2)
␈↓ ↓H␈↓¬(JSP T %XCONS)
␈↓ ↓H␈↓¬G0001
␈↓ ↓H␈↓¬(SUB P (% 0 0 1 1))
␈↓ ↓H␈↓¬(POPJ P)
␈↓ ↓H␈↓¬NIL
␈↓ ↓H␈↓␈↓ ¬qChapter VII␈↓ ≠123
␈↓ ↓H␈↓3. ␈↓αLCOM0.␈↓
␈↓ ↓H␈↓ The␈α∂following␈α∞is␈α∂an␈α∞annotated␈α∂listing␈α∞of␈α∂the␈α∞MACLISP␈α∂version␈α∞of␈α∂LCOM0␈α∂ in␈α∞external
␈↓ ↓H␈↓notation. A listing in internal notation can be found in Appendix B.
␈↓ ↓H␈↓ ␈↓↓compl␈↓␈α∂is␈α∂the␈α∂user-callable␈α∂driver.␈α∂ It␈α∂is␈α∂a␈α∂FEXPR.␈α∂ It␈α∂takes␈α∂as␈α∂an␈α∂argument␈α∂a␈α∂single␈α∂file
␈↓ ↓H␈↓name.␈α⊂ EXPRs␈α⊂on␈α⊂a␈α⊂file␈α∂called␈α⊂FILNAM␈α⊂will␈α⊂be␈α⊂compiled␈α∂into␈α⊂LAP␈α⊂and␈α⊂written␈α⊂on␈α⊂the␈α∂file
␈↓ ↓H␈↓FILNAM.LAP.␈α⊃Other␈α⊃types␈α⊃of␈α⊃function␈α⊃definitions␈α⊃and␈α⊃non-definitions␈α⊃are␈α⊃simply␈α⊃copied␈α⊃to
␈↓ ↓H␈↓output.␈α It␈αis,␈αalas,␈αdependent␈αon␈αthe␈αI/O␈αstructure␈αof␈αthe␈αimplementation␈αand␈αneed␈αnot␈αbe␈αstudied
␈↓ ↓H␈↓carefully. All the other functions are substantially independent of implementation.
␈↓ ↓H␈↓␈↓ α8FEXPER:
␈↓ ↓H␈↓␈↓ α8␈↓↓compl file ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [uwrite[], ␈↓␈↓ λ(~Open a file for output
␈↓ ↓H␈↓␈↓ α8␈↓↓ apply[␈↓¬EREAD␈↓↓, file], ␈↓␈↓ λ(~Open input file
␈↓ ↓H␈↓␈↓ α8␈↓↓ select-disk-input[␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ read-until-eof[with z do␈↓␈↓ λ(~Read each expression in file
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓z = ␈↓¬DEFUN␈↓↓ ∨ [␈↓αa|␈↓↓z = ␈↓¬DEFPROP␈↓↓ ∧ ␈↓αaddd|␈↓↓z = ␈↓¬EXPR␈↓↓] ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [␈↓αprogram␈↓↓ [prog]␈↓
␈↓ ↓H␈↓3.1)␈↓ α8␈↓↓ prog␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ← [␈↓αif␈↓↓ ␈↓αa|␈↓↓z = ␈↓¬DEFUN␈↓↓ ␈↓αthen␈↓↓ comp[␈↓αad|␈↓↓z, ␈↓αadd|␈↓↓z, ␈↓αaddd|␈↓↓z]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ comp[␈↓αad|␈↓↓z, ␈↓αad|␈↓↓␈↓αadd|␈↓↓z, ␈↓αadd|␈↓↓␈↓αadd|␈↓↓z]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ unselect-tty ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ select-disk-output mapc[print, prog]␈↓␈↓ λ(~ Print code in file
␈↓ ↓H␈↓␈↓ α8␈↓↓ print <␈↓αad|␈↓↓z, length prog>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ unselect-tty select-disk-output print z], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ apply[␈↓¬UFILE␈↓↓, <␈↓αa|␈↓↓file, ␈↓¬LAP␈↓↓>], ␈↓␈↓ λ(~Close and name file
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬ENDCOMP␈↓↓]]␈↓
␈↓ ↓H␈↓ ␈↓↓comp␈↓␈αcompiles␈αa␈αsingle␈α
function␈αdefinition,␈αreturning␈αa␈α
list␈αof␈αthe␈αLAP␈α
code␈αcorresponding
␈↓ ↓H␈↓to␈α⊂the␈α⊂definition.␈α⊂ ␈↓↓fn␈↓␈α⊂is␈α⊃the␈α⊂atomic␈α⊂name␈α⊂of␈α⊂the␈α⊃function␈α⊂being␈α⊂compiled.␈α⊂ ␈↓↓vars␈↓␈α⊂is␈α⊃the␈α⊂formal
␈↓ ↓H␈↓parameter list for the function. ␈↓↓exp␈↓ is the function body.
␈↓ ↓H␈↓␈↓ α8␈↓↓comp[fn, vars, exp] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ {length vars}[λn: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <<␈↓¬LAP␈↓↓, fn, ␈↓¬SUBR␈↓↓>>␈↓
␈↓ ↓H␈↓3.2)␈↓ α8␈↓↓ * mkpush[n, 1]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * compexp[exp, -n, prup[vars, 1]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * <<␈↓¬SUB␈↓↓, ␈↓¬P␈↓↓, <␈↓¬%␈↓↓, 0, 0, n, n>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * ␈↓¬((POPJ P) NIL)␈↓↓]␈↓
␈↓ ↓H␈↓ ␈↓↓prup␈↓␈α∩returns␈α∩an␈α∩a-list␈α∩formed␈α∩by␈α⊃pairing␈α∩successive␈α∩elements␈α∩of␈α∩␈↓↓vars␈↓␈α∩with␈α⊃consecutive
␈↓ ↓H␈↓integers beginning with ␈↓↓n.␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓prup[vars, n] ← ␈↓
␈↓ ↓H␈↓3.3)␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓vars ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [␈↓αa|␈↓↓vars . n] . prup[␈↓αd|␈↓↓vars, add1 n]␈↓
␈↓ ↓H␈↓ ␈↓↓mkpush␈↓␈α
returns␈α
a␈αlist␈α
of␈α
␈↓↓n␈↓␈α
␈↓¬(PUSH␈αP␈α
␈↓↓i␈↓¬)␈↓␈α
instructions,␈α
where␈α␈↓↓i␈↓␈α
runs␈α
from␈α
␈↓↓n␈↓␈αto␈α
␈↓↓m+n-1.␈↓␈α
It␈αis␈α
used
␈↓ ↓H␈↓to push arguments onto the stack.
␈↓ ↓H␈↓124␈↓ ¬qChapter VII␈↓ H
␈↓ ↓H␈↓␈↓ α8␈↓↓mkpush[n, m] ← ␈↓
␈↓ ↓H␈↓3.4)␈↓ α8␈↓↓ ␈↓αif␈↓↓ n < m ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ <␈↓¬PUSH␈↓↓, ␈↓¬P␈↓↓, m> . mkpush[n, add1 m]␈↓
␈↓ ↓H␈↓ ␈↓↓compexp␈↓␈α∩is␈α∪the␈α∩heart␈α∩of␈α∪LCOM0.␈α∩ It␈α∪determines␈α∩precisely␈α∩what␈α∪an␈α∩expression␈α∪is,␈α∩and
␈↓ ↓H␈↓compiles␈α⊃appropriate␈α⊃code␈α⊂for␈α⊃it.␈α⊃ It␈α⊂returns␈α⊃a␈α⊃list␈α⊃of␈α⊂that␈α⊃code.␈α⊃ ␈↓↓exp␈↓␈α⊂is␈α⊃the␈α⊃expression␈α⊃to␈α⊂be
␈↓ ↓H␈↓compiled.␈α ␈↓↓m␈↓␈αis␈αminus␈αthe␈αnumber␈αof␈αentries␈αon␈αthe␈αstack.␈αWhen␈αadded␈αto␈αa␈αvalue␈αretrieved␈αfrom
␈↓ ↓H␈↓the␈αA-LIST␈α␈↓↓vpr,␈↓␈αit␈αcan␈αbe␈αused␈αto␈αlocate␈αa␈αvariable␈αon␈αthe␈αstack.␈α ␈↓↓vpr␈↓␈αis␈αan␈αA-LIST,␈αassociating
␈↓ ↓H␈↓variable␈α⊂names␈α⊂with␈α⊂numbers␈α⊂which,␈α⊂when␈α⊂added␈α∂to␈α⊂␈↓↓m,␈↓␈α⊂give␈α⊂stack␈α⊂offsets.␈α⊂ Both␈α⊂␈↓↓m␈↓␈α⊂and␈α∂␈↓↓vpr␈↓
␈↓ ↓H␈↓maintain␈α∂these␈α∂definitions␈α∞throughout.␈α∂ ␈↓↓gensym[]␈↓␈α∂is␈α∂a␈α∞pseudo-function␈α∂of␈α∂no␈α∂arguments.␈α∞ Every
␈↓ ↓H␈↓time␈αit␈αis␈αcalled,␈αit␈αreturns␈αa␈αdifferent␈αsymbol.␈α The␈αpresence␈αof␈α␈↓↓gensym[]␈↓␈αmeans␈αthat␈αLCOM0␈α
and
␈↓ ↓H␈↓LCOM4␈α
are␈α
not␈α
pure␈αLISP␈α
so␈α
that␈α
the␈αtechniques␈α
of␈α
Chapter␈α
3␈α
cannot␈αbe␈α
used␈α
to␈α
prove␈αthat␈α
they
␈↓ ↓H␈↓meet␈α⊂their␈α∂specifications.␈α⊂ It␈α∂is␈α⊂possible␈α∂to␈α⊂eliminate␈α⊂the␈α∂use␈α⊂of␈α∂␈↓↓gensym,␈↓␈α⊂but␈α∂as␈α⊂of␈α⊂the␈α∂present
␈↓ ↓H␈↓writing,␈α
such␈α
version␈α
hasn't␈α
been␈α
written.␈α In␈α
any␈α
case,␈α
other␈α
unsolved␈α
problems␈α
remain␈αbefore␈α
one
␈↓ ↓H␈↓can␈α
give␈α
a␈α
first␈α
order␈αlogic␈α
proof␈α
of␈α
the␈α
correspondence␈αbetween␈α
␈↓↓eval␈↓␈α
and␈α
the␈α
code␈α
produced␈αby
␈↓ ↓H␈↓the compilers.
␈↓ ↓H␈↓␈↓ α8␈↓↓compexp[exp, m, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓exp ␈↓αthen␈↓↓ ␈↓¬((MOVEI 1 0))␈↓↓␈↓␈↓ _~␈↓¬NIL␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ exp = ␈↓¬T␈↓↓ ␈↓αthen␈↓↓ ␈↓¬((MOVEI 1 (QUOTE T)))␈↓↓␈↓␈↓ _~␈↓¬T␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ numberp exp ␈↓αthen␈↓↓ <<␈↓¬MOVEI␈↓↓, 1, <␈↓¬QUOTE␈↓↓, exp>>>␈↓␈↓ _~number
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓exp ␈↓αthen␈↓↓ <<␈↓¬MOVE␈↓↓, 1, m + ␈↓αd|␈↓↓assoc[exp, vpr], ␈↓¬P␈↓↓>>␈↓␈↓ _~variable
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬AND␈↓↓ ∨ ␈↓αa|␈↓↓exp = ␈↓¬OR␈↓↓ ∨ ␈↓αa|␈↓↓exp = ␈↓¬NOT␈↓↓ ␈↓αthen␈↓↓ ␈↓␈↓ _~boolean expression
␈↓ ↓H␈↓␈↓ α8␈↓↓ {gensym[], gensym[]}[λl1, l2: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ combool[exp, m, l1, ␈↓¬NIL␈↓↓, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * <␈↓¬(MOVEI 1 (QUOTE T))␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <␈↓¬JRST␈↓↓, 0, l2>, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ l1, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬(MOVEI 1 0)␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ l2>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬COND␈↓↓ ␈↓αthen␈↓↓ comcond[␈↓αd|␈↓↓exp, m, gensym[], vpr]␈↓␈↓ _~␈↓¬COND ␈↓
␈↓ ↓H␈↓3.5)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬QUOTE␈↓↓ ␈↓αthen␈↓↓ <<␈↓¬MOVEI␈↓↓, 1, exp>>␈↓␈↓ _~␈↓¬QUOTE ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓exp ␈↓αthen␈↓↓ ␈↓␈↓ _~function call
␈↓ ↓H␈↓␈↓ α8␈↓↓ {length ␈↓αd|␈↓↓exp}[λn: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ complis[␈↓αd|␈↓↓exp, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * loadac[1 - n, 1]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * <<␈↓¬SUB␈↓↓, ␈↓¬P␈↓↓, <␈↓¬%␈↓↓, 0, 0, n, n>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * <<␈↓¬CALL␈↓↓, n, <␈↓¬QUOTE␈↓↓, ␈↓αa|␈↓↓exp>>>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓exp = ␈↓¬LAMBDA␈↓↓ ␈↓αthen␈↓↓ ␈↓␈↓ _~λ-expression
␈↓ ↓H␈↓␈↓ α8␈↓↓ {length ␈↓αd|␈↓↓exp}[λn: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ complis[␈↓αd|␈↓↓exp, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * compexp[␈↓αadda|␈↓↓exp, m - n, prup[␈↓αada|␈↓↓exp, 1 - m] * vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * <<␈↓¬SUB␈↓↓, ␈↓¬P␈↓↓, <␈↓¬%␈↓↓, 0, 0, n, n>>>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓␈↓␈↓ _~oops!
␈↓ ↓H␈↓ ␈↓↓complis␈↓␈αcompiles␈αcode␈αto␈αevaluate␈αeach␈αexpression␈αin␈αa␈αlist␈αof␈αexpressions␈αand␈αto␈αpush␈αthose
␈↓ ↓H␈↓values␈α⊃onto␈α⊃the␈α⊃stack.␈α⊃ It␈α⊃returns␈α⊂a␈α⊃list␈α⊃of␈α⊃that␈α⊃code.␈α⊃ It␈α⊂is␈α⊃used␈α⊃to␈α⊃compile␈α⊃code␈α⊃to␈α⊂evaluate
␈↓ ↓H␈↓arguments to called functions or λ-expressions. ␈↓↓u␈↓ is a list of expressions.
␈↓ ↓H␈↓␈↓ α8␈↓↓complis[u, m, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬qChapter VII␈↓ ≠125
␈↓ ↓H␈↓3.6)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ compexp[␈↓αa|␈↓↓u, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * ␈↓¬((PUSH P 1))␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * complis[␈↓αd|␈↓↓u, sub1 m, vpr]␈↓
␈↓ ↓H␈↓␈↓↓loadac␈↓␈αreturns␈αa␈αlist␈αof␈α␈↓¬(MOVE␈α␈↓↓i␈↓¬␈α␈↓↓j␈↓¬␈αP)␈↓␈αinstructions,␈αloading␈αconsecutive␈αaccumulators␈αfrom␈αthe␈αtop␈αof
␈↓ ↓H␈↓the stack. ␈↓↓k␈↓ indexes the accumulator loaded. ␈↓↓n␈↓ indexes the stack offset.
␈↓ ↓H␈↓␈↓ α8␈↓↓loadac[n, k] ← ␈↓
␈↓ ↓H␈↓3.7)␈↓ α8␈↓↓ ␈↓αif␈↓↓ n > 0 ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ <␈↓¬MOVE␈↓↓, k, n, ␈↓¬P␈↓↓> . loadac[add1 n, add1 k]␈↓
␈↓ ↓H␈↓ ␈↓↓comcond␈↓␈αcompiles␈α
a␈α␈↓¬COND.␈α
␈↓␈α␈↓↓u␈↓␈α
is␈αa␈α
list␈αof␈α
clauses␈αin␈α
the␈α␈↓¬COND.␈α
␈↓␈α␈↓↓l␈↓␈α
is␈αa␈α
label␈αto␈α
be␈αemitted␈αat␈α
the
␈↓ ↓H␈↓end of all code for the ␈↓¬COND. ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓comcond[u, m, l, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ <l>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ {gensym[]}[λl1: ␈↓
␈↓ ↓H␈↓3.8)␈↓ α8␈↓↓ combool[␈↓αaa|␈↓↓u, m, l1, ␈↓¬NIL␈↓↓, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * compexp[␈↓αada|␈↓↓u, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * <<␈↓¬JRST␈↓↓, 0, l>, l1>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * comcond[␈↓αd|␈↓↓u, m, l, vpr]]␈↓
␈↓ ↓H␈↓ ␈↓↓combool␈↓␈α⊂compiles␈α∂code␈α⊂for␈α∂a␈α⊂single␈α∂propositional␈α⊂expression.␈α∂ That␈α⊂is,␈α∂the␈α⊂code␈α∂generated
␈↓ ↓H␈↓evaluates␈αthe␈αexpression␈αand␈αbranches␈αsomewhere,␈αaccording␈αto␈αwhether␈αits␈αvalue␈αis␈αtrue␈αor␈αfalse.
␈↓ ↓H␈↓␈↓↓p␈↓␈αis␈αthe␈αpropositional␈αexpression.␈α ␈↓↓l␈↓␈αis␈αa␈αlabel␈αwhich␈αrepresents␈αthe␈αbranch␈αpoint.␈α ␈↓↓flg␈↓␈αis␈αa␈αflag.␈α If
␈↓ ↓H␈↓␈↓↓flg␈↓␈α
is␈α
␈↓¬NIL␈↓,␈αcode␈α
is␈α
to␈αfall␈α
thru␈α
on␈αnon-␈↓¬NIL␈↓␈α
result␈α
and␈αbranch␈α
to␈α
␈↓↓l␈↓␈αon␈α
␈↓¬NIL␈↓␈α
result.␈α If␈α
␈↓↓flg␈↓␈α
is␈αnon-␈↓¬NIL␈↓,
␈↓ ↓H␈↓code is to fall thru on ␈↓¬NIL␈↓ result and branch to ␈↓↓l␈↓ on non-␈↓¬NIL␈↓ result.
␈↓ ↓H␈↓␈↓ α8␈↓↓combool[p, m, l, flg, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓p ␈↓αthen␈↓↓ ␈↓␈↓ _~simple variable
␈↓ ↓H␈↓␈↓ α8␈↓↓ [compexp[p, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * <<␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ ␈↓¬JUMPN␈↓↓ ␈↓αelse␈↓↓ ␈↓¬JUMPE␈↓↓, 1, l>>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬AND␈↓↓ ␈↓αthen␈↓↓ ␈↓␈↓ _~conjunction
␈↓ ↓H␈↓␈↓ α8␈↓↓ [␈↓αif␈↓↓ ¬flg ␈↓αthen␈↓↓ compandor[␈↓αd|␈↓↓p, m, l, ␈↓¬NIL␈↓↓, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ {gensym[]}[λl1: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ compandor[␈↓αd|␈↓↓p, m, l1, ␈↓¬NIL␈↓↓, vpr]␈↓
␈↓ ↓H␈↓3.9)␈↓ α8␈↓↓ * <<␈↓¬JRST␈↓↓, 0, l>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * <l1>]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬OR␈↓↓ ␈↓αthen␈↓↓ ␈↓␈↓ _~disjunction
␈↓ ↓H␈↓␈↓ α8␈↓↓ [␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ compandor[␈↓αd|␈↓↓p, m, l, ␈↓¬T␈↓↓, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ {gensym[]}[λl1: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ compandor[␈↓αd|␈↓↓p, m, l1, ␈↓¬T␈↓↓, vpr] * <<␈↓¬JRST␈↓↓, 0, l>> * <l1>]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬NOT␈↓↓ ␈↓αthen␈↓↓ combool[␈↓αad|␈↓↓p, m, l, ¬flg, vpr]␈↓␈↓ _~negation
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ compexp[p, m, vpr]␈↓␈↓ _~other expression
␈↓ ↓H␈↓␈↓ α8␈↓↓ * <<␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ ␈↓¬JUMPN␈↓↓ ␈↓αelse␈↓↓ ␈↓¬JUMPE␈↓↓, 1, l>>␈↓
␈↓ ↓H␈↓ ␈↓↓compandor␈↓␈α
compiles␈αcode␈α
for␈α
lists␈αof␈α
predicates␈α
connected␈αconjunctively␈α
or␈α
disjunctively.␈α ␈↓↓u␈↓
␈↓ ↓H␈↓is␈α
a␈αlist␈α
of␈α
predicates.␈α ␈↓↓l␈↓␈α
is␈αa␈α
label.␈α
␈↓↓flg␈↓␈αis␈α
a␈α
flag.␈α If␈α
␈↓↓flg␈↓␈αis␈α
␈↓¬NIL␈↓,␈α
we␈αare␈α
to␈α
fall␈αthru␈α
on␈αnon-␈↓¬NIL␈↓␈α
results
␈↓ ↓H␈↓and␈αbranch␈αto␈α␈↓↓l␈↓␈αon␈α␈↓¬NIL␈↓␈αresults␈α(␈↓¬AND␈α␈↓case).␈α If␈α␈↓↓flg␈↓␈αis␈αnon-␈↓¬NIL␈↓,␈αwe␈αare␈αto␈αfall␈αthru␈αon␈α␈↓¬NIL␈↓␈αresults␈αand
␈↓ ↓H␈↓branch to ␈↓↓l␈↓ on non-␈↓¬NIL␈↓ results (␈↓¬OR ␈↓case).
␈↓ ↓H␈↓126␈↓ ¬qChapter VII␈↓ H
␈↓ ↓H␈↓␈↓ α8␈↓↓compandor[u, m, l, flg, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓3.10)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ combool[␈↓αa|␈↓↓u, m, l, flg, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ * compandor[␈↓αd|␈↓↓u, m, l, flg, vpr]␈↓
␈↓ ↓H␈↓␈↓ ¬qChapter VII␈↓ ≠127
␈↓ ↓H␈↓4. ␈↓αLCOM4.␈↓
␈↓ ↓H␈↓ The␈α∞following␈α∞is␈α∞a␈α∂listing␈α∞in␈α∞external␈α∞notation␈α∞of␈α∂the␈α∞MACLISP␈α∞version␈α∞of␈α∂the␈α∞compiler
␈↓ ↓H␈↓LCOM4.␈α∂ A␈α∂listing␈α∂in␈α⊂internal␈α∂notation␈α∂can␈α∂be␈α⊂found␈α∂in␈α∂Appendix B.␈α∂ Comments␈α⊂are␈α∂added
␈↓ ↓H␈↓where␈αthis␈αcompiler␈αdiffers␈αfrom␈αLCOM0.␈α The␈αmain␈αdifference␈αhas␈αto␈αdo␈αwith␈αthe␈αclassification
␈↓ ↓H␈↓of␈α
expressions␈α
into␈α
five␈α∞classes␈α
that␈α
can␈α
be␈α
compiled␈α∞differently␈α
for␈α
greater␈α
efficiency.␈α∞ The␈α
five
␈↓ ↓H␈↓classes are defined as follows:
␈↓ ↓H␈↓ 0. constants ␈↓¬NIL␈↓, ␈↓¬T␈↓, and numbers,
␈↓ ↓H␈↓ 1. variables -- any atom not considered a constant,
␈↓ ↓H␈↓ 2. quoted S-expressions -- non-atomic constants,
␈↓ ↓H␈↓ 3. ␈↓↓car␈↓ - ␈↓↓cdr␈↓ chains applied to a variable,
␈↓ ↓H␈↓ 4. all others, except
␈↓ ↓H␈↓ 5. the last class 4 element in the argument list.
␈↓ ↓H␈↓ FEXPR:
␈↓ ↓H␈↓␈↓ α8␈↓↓compl file ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [uwrite[], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ apply[␈↓¬EREAD␈↓↓, file], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ select-disk-input[␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ read-until-eof[with z do␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓z = ␈↓¬DEFUN␈↓↓ ∨ [␈↓αa|␈↓↓z = ␈↓¬DEFPROP␈↓↓ ∧ ␈↓αaddd|␈↓↓z = ␈↓¬EXPR␈↓↓] ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [␈↓αprogram␈↓↓ [prog]␈↓
␈↓ ↓H␈↓4.1)␈↓ α8␈↓↓ prog␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ← [␈↓αif␈↓↓ ␈↓αa|␈↓↓z = ␈↓¬DEFUN␈↓↓ ␈↓αthen␈↓↓ comp[␈↓αad|␈↓↓z, ␈↓αadd|␈↓↓z, ␈↓αaddd|␈↓↓z]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ comp[␈↓αad|␈↓↓z, ␈↓αad|␈↓↓␈↓αadd|␈↓↓z, ␈↓αadd|␈↓↓␈↓αadd|␈↓↓z]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ unselect-tty ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ select-disk-output mapc[print, prog]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ print <␈↓αad|␈↓↓z, length prog>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ unselect-tty select-disk-output print z], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ apply[␈↓¬UFILE␈↓↓, <␈↓αa|␈↓↓file, ␈↓¬LAP␈↓↓>], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬ENDCOMP␈↓↓]]␈↓
␈↓ ↓H␈↓ Instead␈α∞of␈α∂␈↓↓append␈↓ing␈α∞the␈α∂instructions␈α∞into␈α∂a␈α∞list␈α∂like␈α∞␈↓↓compexp␈↓␈α∂of␈α∞LCOM0␈α∂does,␈α∞LCOM4
␈↓ ↓H␈↓␈↓↓cons␈↓es␈αthem␈αinto␈αa␈α
tree␈αform␈αand␈αflattens␈αthe␈α
final␈αversion␈αinto␈αa␈αlist.␈α
In␈αorder␈αthat␈αthe␈α
items␈αof
␈↓ ↓H␈↓the␈α
tree␈αform␈α
all␈α
have␈αthe␈α
structure␈α
of␈αa␈α
list␈αof␈α
atoms,␈α
labels␈αand␈α
the␈α
end␈αmark␈α
␈↓¬NIL␈↓␈α
are␈αput␈α
in␈αa␈α
list
␈↓ ↓H␈↓flagged␈αby␈α␈↓¬LABEL␈α
␈↓and␈αextracted␈αby␈α␈↓↓flat.␈↓␈α
The␈αhope␈αwas␈αto␈α
make␈αthe␈αcompiler␈αfaster␈α
by␈αomitting
␈↓ ↓H␈↓all the calls to ␈↓↓append␈↓ but it does not seem to have made a lot of difference.
␈↓ ↓H␈↓␈↓ α8␈↓↓comp[fn, vars, exp] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ {prup[vars, 1], length vars}[λvpr, n: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ flat[<<<␈↓¬LAP␈↓↓, fn, ␈↓¬SUBR␈↓↓>>, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ mkpush[n, 1], ␈↓
␈↓ ↓H␈↓4.2)␈↓ α8␈↓↓ compexp[exp, -n, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ substack n, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬((POPJ P) (LABEL NIL))␈↓↓>, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬NIL␈↓↓]]␈↓
␈↓ ↓H␈↓128␈↓ ¬qChapter VII␈↓ H
␈↓ ↓H␈↓␈↓ α8␈↓↓flat[u, s] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ s␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αa|␈↓↓u ␈↓αthen␈↓↓ flat[␈↓αd|␈↓↓u, s]␈↓
␈↓ ↓H␈↓4.3)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬LABEL␈↓↓ ␈↓αthen␈↓↓ ␈↓αad|␈↓↓u . s␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓u ␈↓αthen␈↓↓ u . s␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ flat[␈↓αa|␈↓↓u, flat[␈↓αd|␈↓↓u, s]]␈↓
␈↓ ↓H␈↓ ␈↓↓substack␈↓␈αcreates␈αcode␈αto␈αmove␈αdown␈αthe␈αstack,␈αbut␈αcreates␈αno␈αcode␈αfor␈αthe␈αcase␈α␈↓↓n␈↓␈α=␈α0␈αwhich
␈↓ ↓H␈↓is a no-op.
␈↓ ↓H␈↓␈↓ α8␈↓↓substack n ← ␈↓
␈↓ ↓H␈↓4.4)␈↓ α8␈↓↓ ␈↓αif␈↓↓ n = 0 ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ <<␈↓¬SUB␈↓↓, ␈↓¬P␈↓↓, <␈↓¬%␈↓↓, 0, 0, n, n>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓prup[vars, n] ← ␈↓
␈↓ ↓H␈↓4.5)␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓vars ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [␈↓αa|␈↓↓vars . n] . prup[␈↓αd|␈↓↓vars, add1 n]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓mkpush[n, m] ← ␈↓
␈↓ ↓H␈↓4.6)␈↓ α8␈↓↓ ␈↓αif␈↓↓ n < m ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ <␈↓¬PUSH␈↓↓, ␈↓¬P␈↓↓, m> . mkpush[n, add1 m]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓compexp[exp, m, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓exp ␈↓αthen␈↓↓ ␈↓¬((MOVEI 1 0))␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ exp = ␈↓¬T␈↓↓ ∨ numberp exp ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <<␈↓¬MOVEI␈↓↓, 1, <␈↓¬QUOTE␈↓↓, exp>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓exp ␈↓αthen␈↓↓ <<␈↓¬MOVE␈↓↓, 1, m + ␈↓αd|␈↓↓assoc[exp, vpr], ␈↓¬P␈↓↓>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬CAR␈↓↓ ␈↓αthen␈↓↓ ␈↓␈↓ λx~compute ␈↓↓car␈↓ directly
␈↓ ↓H␈↓␈↓ α8␈↓↓ [␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αad|␈↓↓exp ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <<␈↓¬HLRZ␈↓↓, 1, ␈↓¬@␈↓↓, m + ␈↓αd|␈↓↓assoc[␈↓αad|␈↓↓exp, vpr], ␈↓¬P␈↓↓>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ <compexp[␈↓αad|␈↓↓exp, m, vpr], ␈↓¬((HLRZ 1 @ 1))␈↓↓>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬CDR␈↓↓ ␈↓αthen␈↓↓ ␈↓␈↓ λx~compute ␈↓↓cdr␈↓ directly
␈↓ ↓H␈↓␈↓ α8␈↓↓ [␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αad|␈↓↓exp ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <<␈↓¬HRRZ␈↓↓, 1, ␈↓¬@␈↓↓, m + ␈↓αd|␈↓↓assoc[␈↓αad|␈↓↓exp, vpr], ␈↓¬P␈↓↓>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ <compexp[␈↓αad|␈↓↓exp, m, vpr], ␈↓¬((HRRZ 1 @ 1))␈↓↓>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬AND␈↓↓ ∨ ␈↓αa|␈↓↓exp = ␈↓¬OR␈↓↓ ∨ ␈↓αa|␈↓↓exp = ␈↓¬NOT␈↓↓ ∨ ␈↓αa|␈↓↓exp = ␈↓¬EQ␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αthen␈↓↓ {gensym[], gensym[]}[λl1, l2: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <combool[exp, m, l1, ␈↓¬NIL␈↓↓, vpr], ␈↓
␈↓ ↓H␈↓4.7)␈↓ α8␈↓↓ <␈↓¬(MOVEI 1 (QUOTE T))␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <␈↓¬JRST␈↓↓, 0, l2>, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <␈↓¬LABEL␈↓↓, l1>, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬(MOVEI 1 0)␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <␈↓¬LABEL␈↓↓, l2>>>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬COND␈↓↓ ␈↓αthen␈↓↓ comcond[␈↓αd|␈↓↓exp, m, gensym[], vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬QUOTE␈↓↓ ␈↓αthen␈↓↓ <<␈↓¬MOVEI␈↓↓, 1, exp>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓exp ␈↓αthen␈↓↓ ␈↓␈↓ λx~uses improved
␈↓ ↓H␈↓␈↓ α8␈↓↓ <complisa[␈↓αd|␈↓↓exp, m, vpr], ␈↓␈↓ λx~argument evaluation
␈↓ ↓H␈↓␈↓ α8␈↓↓ <<␈↓¬CALL␈↓↓, length ␈↓αd|␈↓↓exp, <␈↓¬QUOTE␈↓↓, ␈↓αa|␈↓↓exp>>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓exp = ␈↓¬LAMBDA␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ {length ␈↓αd|␈↓↓exp}[λn: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <stackup[␈↓αd|␈↓↓exp, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ compexp[␈↓αadda|␈↓↓exp, m - n, prup[␈↓αada|␈↓↓exp, 1 - m] * vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ substack n>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬qChapter VII␈↓ ≠129
␈↓ ↓H␈↓␈↓ α8␈↓↓stackup[u, m, vpr] ← ␈↓␈↓ λx␈↓↓~␈↓ ≡ ␈↓↓complis␈↓ of LCOM0.
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓4.8)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ <compexp[␈↓αa|␈↓↓u, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬((PUSH P 1))␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ stackup[␈↓αd|␈↓↓u, sub1 m, vpr]>␈↓
␈↓ ↓H␈↓ ␈↓↓ccchain␈↓ test whether an expression is a ␈↓↓car␈↓ - ␈↓↓cdr␈↓ chain applied to a variable.
␈↓ ↓H␈↓␈↓ α8␈↓↓ccchain exp ← ␈↓
␈↓ ↓H␈↓4.9)␈↓ α8␈↓↓ [␈↓αa|␈↓↓exp = ␈↓¬CAR␈↓↓ ∨ ␈↓αa|␈↓↓exp = ␈↓¬CDR␈↓↓] ∧ [␈↓αat|␈↓↓␈↓αad|␈↓↓exp ∨ ccchain ␈↓αad|␈↓↓exp]␈↓
␈↓ ↓H␈↓ ␈↓↓compc␈↓␈α⊂generates␈α∂code␈α⊂(in␈α∂reverse␈α⊂order)␈α⊂to␈α∂load␈α⊂the␈α∂value␈α⊂of␈α⊂a␈α∂␈↓↓car␈↓␈α⊂-␈α∂␈↓↓cdr␈↓␈α⊂chain␈α⊂into␈α∂the
␈↓ ↓H␈↓accumulator designated by ␈↓↓n2.␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓compc[exp, n2, m, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓exp ␈↓αthen␈↓↓ error ␈↓¬COMPC␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓exp = ␈↓¬CAR␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αad|␈↓↓exp ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓4.10)␈↓ α8␈↓↓ <<␈↓¬HLRZ␈↓↓, n2, ␈↓¬@␈↓↓, m + ␈↓αd|␈↓↓assoc[␈↓αad|␈↓↓exp, vpr], ␈↓¬P␈↓↓>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ <␈↓¬HLRZ␈↓↓, n2, ␈↓¬@␈↓↓, n2> . compc[␈↓αad|␈↓↓exp, n2, m, vpr]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αad|␈↓↓exp ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <<␈↓¬HRRZ␈↓↓, ␈↓¬@␈↓↓, n2, m + ␈↓αd|␈↓↓assoc[␈↓αad|␈↓↓exp, vpr], ␈↓¬P␈↓↓>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ <␈↓¬HRRZ␈↓↓, n2, ␈↓¬@␈↓↓, n2> . compc[␈↓αad|␈↓↓exp, n2, m, vpr]␈↓
␈↓ ↓H␈↓ ␈↓↓comcond␈↓␈αis␈αessentially␈αthe␈αsame␈αas␈αin␈αLCOM0.␈α It␈αoptimizes␈αby␈αconsidering␈αthe␈αspecial␈αcases
␈↓ ↓H␈↓where␈αthe␈αnext␈αclause␈αin␈αthe␈αlist␈αof␈αarguments␈αto␈α␈↓¬COND␈α␈↓␈αis␈αof␈αthe␈αform␈α␈↓¬((NULL ␈↓↓e␈↓¬) NIL)␈↓␈α or␈α␈↓¬(T ␈↓↓e␈↓¬)␈↓␈α for
␈↓ ↓H␈↓some expression ␈↓↓e.␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓comcond[u, m, l, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ <<␈↓¬LABEL␈↓↓, l>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ¬␈↓αat|␈↓↓␈↓αaa|␈↓↓u ∧ ␈↓αaaa|␈↓↓u = ␈↓¬NULL␈↓↓ ∧ ␈↓αn|␈↓↓␈↓αada|␈↓↓u ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <compexp[␈↓αadaa|␈↓↓u, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <<␈↓¬JUMPE␈↓↓, 1, l>>, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ comcond[␈↓αd|␈↓↓u, m, l, vpr]>␈↓
␈↓ ↓H␈↓4.11)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓u = ␈↓¬T␈↓↓ ␈↓αthen␈↓↓ <compexp[␈↓αada|␈↓↓u, m, vpr], <<␈↓¬LABEL␈↓↓, l>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ {gensym[]}[λl1: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <combool[␈↓αaa|␈↓↓u, m, l1, ␈↓¬NIL␈↓↓, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ compexp[␈↓αada|␈↓↓u, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <<␈↓¬JRST␈↓↓, 0, l>, <␈↓¬LABEL␈↓↓, l1>>, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ comcond[␈↓αd|␈↓↓u, m, l, vpr]>]␈↓
␈↓ ↓H␈↓ ␈↓↓complisa␈↓␈αclassifies␈αthe␈αexpressions␈αon␈αthe␈αargument␈αlist␈α␈↓↓u,␈↓␈αcalls␈α␈↓↓complis␈↓␈αto␈αgenerate␈αcode␈αfor
␈↓ ↓H␈↓those␈α∞arguments␈α∂which␈α∞must␈α∂be␈α∞computed␈α∂and␈α∞stored␈α∂on␈α∞the␈α∂stack,␈α∞␈↓↓loadac␈↓␈α∂to␈α∞generate␈α∂code␈α∞for
␈↓ ↓H␈↓loading the accumulators and ␈↓↓substack␈↓ to generate code to restore the stack.
␈↓ ↓H␈↓␈↓ α8␈↓↓complisa[u, m, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ {classify u}[λz: ␈↓
␈↓ ↓H␈↓4.12)␈↓ α8␈↓↓ <complis[z, m, 1, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ loadac[z, 1 - ccount z, 1, m - ccount z, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ substack ccount z>]␈↓
␈↓ ↓H␈↓130␈↓ ¬qChapter VII␈↓ H
␈↓ ↓H␈↓ ␈↓↓ccount␈↓␈αof␈α
a␈αlist␈α
of␈αclassified␈αexpressions␈α
is␈αthe␈α
number␈αof␈αexpressions␈α
on␈αthe␈α
list␈αof␈α
class␈α4.
␈↓ ↓H␈↓That is the number of values that will have to be stacked when evaluating ␈↓↓z␈↓ as an argument list.
␈↓ ↓H␈↓␈↓ α8␈↓↓ccount z ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓z ␈↓αthen␈↓↓ 0␈↓
␈↓ ↓H␈↓4.13)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 4 ␈↓αthen␈↓↓ add1 ccount ␈↓αd|␈↓↓z␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ccount ␈↓αd|␈↓↓z␈↓
␈↓ ↓H␈↓ ␈↓↓loadac␈↓␈αcompiles␈αcode␈αto␈αload␈αaccumulators␈α␈↓↓n2␈↓␈αand␈αup␈αwith␈αthe␈αvalues␈αof␈αthe␈αexpressions␈αon
␈↓ ↓H␈↓the␈α⊃list␈α∩␈↓↓z␈↓␈α⊃of␈α∩classified␈α⊃expressions.␈α∩ ␈↓↓m2␈↓␈α⊃is␈α∩the␈α⊃stack␈α⊃offset␈α∩for␈α⊃the␈α∩value␈α⊃of␈α∩the␈α⊃next␈α∩class␈α⊃4
␈↓ ↓H␈↓expression on the list.
␈↓ ↓H␈↓␈↓ α8␈↓↓loadac[z, m2, n2, m, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓z ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 1 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <␈↓¬MOVE␈↓↓, n2, m + ␈↓αd|␈↓↓assoc[␈↓αda|␈↓↓z, vpr], ␈↓¬P␈↓↓>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ . loadac[␈↓αd|␈↓↓z, m2, add1 n2, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 0 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <␈↓¬MOVEI␈↓↓, n2, <␈↓¬QUOTE␈↓↓, ␈↓αda|␈↓↓z>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ . loadac[␈↓αd|␈↓↓z, m2, add1 n2, m, vpr]␈↓
␈↓ ↓H␈↓4.14)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 2 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <␈↓¬MOVEI␈↓↓, n2, ␈↓αda|␈↓↓z> . loadac[␈↓αd|␈↓↓z, m2, add1 n2, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 3 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <reverse compc[␈↓αda|␈↓↓z, n2, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ loadac[␈↓αd|␈↓↓z, m2, add1 n2, m, vpr]>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 5 ␈↓αthen␈↓↓ loadac[␈↓αd|␈↓↓z, 1, add1 n2, m, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ <␈↓¬MOVE␈↓↓, n2, m2, ␈↓¬P␈↓↓>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ . loadac[␈↓αd|␈↓↓z, add1 m2, add1 n2, m, vpr]␈↓
␈↓ ↓H␈↓ ␈↓↓complis␈↓␈αgenerates␈αcode␈αto␈αstack␈αthe␈αvalues␈αof␈αthe␈αclass␈α4␈αexpressions␈αon␈αthe␈αlist␈αof␈αclassified
␈↓ ↓H␈↓expressions␈α␈↓↓z.␈↓␈α ␈↓↓k␈↓␈αis␈αthe␈αaccumulator␈αwhere␈αthe␈αnext␈αvalue␈αshould␈αgo.␈α The␈αlast␈αclass 4␈αexpression
␈↓ ↓H␈↓is␈α∞marked␈α∞class 5.␈α∞ When␈α∞this␈α∞is␈α∞evaluated␈α∞it␈α∞is␈α∞loaded␈α∞directly␈α∞rather␈α∞than␈α∞being␈α∞stacked.␈α∞ If␈α
a
␈↓ ↓H␈↓class 5 expression is encountered, ␈↓↓complis␈↓ can quit as it has no more work.
␈↓ ↓H␈↓␈↓ α8␈↓↓complis[z, m, k, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓z ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 4 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <compexp[␈↓αda|␈↓↓z, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓¬((PUSH P 1))␈↓↓, ␈↓
␈↓ ↓H␈↓4.15)␈↓ α8␈↓↓ complis[␈↓αd|␈↓↓z, sub1 m, add1 k, vpr]>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓z = 5 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <compexp[␈↓αda|␈↓↓z, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ k = 1 ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ <<␈↓¬MOVE␈↓↓, k, 1>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ complis[␈↓αd|␈↓↓z, m, add1 k, vpr]␈↓
␈↓ ↓H␈↓ ␈↓↓classify␈↓␈αtakes␈αa␈αlist␈αof␈αexpressions␈αand␈αreturns␈αa␈αcorresponding␈αlist␈αof␈α
classified␈αexpressions
␈↓ ↓H␈↓of␈αthe␈αform␈α[␈↓↓n␈↓␈α
.␈α␈↓↓e]␈↓␈αwhere␈α␈↓↓n␈↓␈α
is␈αthe␈αclass␈αof␈α␈↓↓e␈↓␈α
according␈αto␈αthe␈αrules␈α
given␈αat␈αthe␈αbeginning␈α
of␈αthe
␈↓ ↓H␈↓listing. The last class 4 expression on the list is numbered 5.
␈↓ ↓H␈↓4.16)␈↓ α8␈↓↓classify u ← class2[class1[u, ␈↓¬NIL␈↓↓], ␈↓¬NIL␈↓↓, ␈↓¬T␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬qChapter VII␈↓ ≠131
␈↓ ↓H␈↓␈↓ α8␈↓↓class1[u, v] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓u ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬NIL␈↓↓ ∨ ␈↓αa|␈↓↓u = ␈↓¬T␈↓↓ ∨ numberp ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓4.17)␈↓ α8␈↓↓ class1[␈↓αd|␈↓↓u, [0 . ␈↓αa|␈↓↓u] . v]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ class1[␈↓αd|␈↓↓u, [1 . ␈↓αa|␈↓↓u] . v]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓u = ␈↓¬QUOTE␈↓↓ ␈↓αthen␈↓↓ class1[␈↓αd|␈↓↓u, [2 . ␈↓αa|␈↓↓u] . v]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ccchain ␈↓αa|␈↓↓u ␈↓αthen␈↓↓ class1[␈↓αd|␈↓↓u, [3 . ␈↓αa|␈↓↓u] . v]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ class1[␈↓αd|␈↓↓u, [4 . ␈↓αa|␈↓↓u] . v]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓class2[u, v, flg] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v␈↓
␈↓ ↓H␈↓4.18)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ flg ∧ ␈↓αaa|␈↓↓u = 4 ␈↓αthen␈↓↓ class2[␈↓αd|␈↓↓u, [5 . ␈↓αda|␈↓↓u] . v, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ class2[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u . v, flg]␈↓
␈↓ ↓H␈↓4.19)␈↓ α8␈↓↓mkjrst l ← <<␈↓¬JRST␈↓↓, 0, l>>␈↓
␈↓ ↓H␈↓ ␈↓↓combool␈↓␈α
is␈α
essentially␈αthe␈α
same␈α
as␈α
in␈αLCOM0␈α
except␈α
it␈α
treats␈αadditional␈α
special␈α
cases␈αwhere␈α
␈↓↓p␈↓
␈↓ ↓H␈↓is␈αof␈αthe␈αform␈α␈↓¬T␈↓,␈α␈↓¬(EQ␈α␈↓↓e1␈↓¬␈α␈↓↓e2␈↓¬)␈↓,␈α
or␈α␈↓¬(NULL␈α␈↓↓e␈↓¬)␈↓.␈α It␈αalso␈αhandles␈αthe␈αjumping␈αsomewhat␈α
differently␈αusing
␈↓ ↓H␈↓two versions of ␈↓↓compandor.␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓combool[p, m, l, flg, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ p = ␈↓¬T␈↓↓ ␈↓αthen␈↓↓ [␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ mkjrst l ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓p ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <compexp[p, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <<␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ ␈↓¬JUMPN␈↓↓ ␈↓αelse␈↓↓ ␈↓¬JUMPE␈↓↓, 1, l>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬EQ␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <complisa[␈↓αd|␈↓↓p, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ ␈↓¬((CAMN 1 2))␈↓↓ ␈↓αelse␈↓↓ ␈↓¬((CAME 1 2))␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ mkjrst l>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬AND␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [␈↓αif␈↓↓ ¬flg ␈↓αthen␈↓↓ compandor[␈↓αd|␈↓↓p, m, l, ␈↓¬NIL␈↓↓, vpr]␈↓
␈↓ ↓H␈↓4.20)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ {gensym[]}[λl1: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <compandor1[␈↓αd|␈↓↓p, m, l1, l, ␈↓¬NIL␈↓↓, vpr], <<␈↓¬LABEL␈↓↓, l1>>>]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬OR␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ compandor[␈↓αd|␈↓↓p, m, l, ␈↓¬T␈↓↓, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ {gensym[]}[λl1: ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <compandor1[␈↓αd|␈↓↓p, m, l1, l, ␈↓¬T␈↓↓, vpr], <<␈↓¬LABEL␈↓↓, l1>>>]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬NOT␈↓↓ ␈↓αthen␈↓↓ combool[␈↓αad|␈↓↓p, m, l, ¬flg, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓p = ␈↓¬NULL␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <compexp[␈↓αad|␈↓↓p, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <<␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ ␈↓¬JUMPE␈↓↓ ␈↓αelse␈↓↓ ␈↓¬JUMPN␈↓↓, 1, l>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ <compexp[p, m, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ <<␈↓αif␈↓↓ flg ␈↓αthen␈↓↓ ␈↓¬JUMPN␈↓↓ ␈↓αelse␈↓↓ ␈↓¬JUMPE␈↓↓, 1, l>>>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓compandor[u, m, l, flg, vpr] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓4.21)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ <combool[␈↓αa|␈↓↓u, m, l, flg, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ compandor[␈↓αd|␈↓↓u, m, l, flg, vpr]>␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓compandor1[u, m, l, l2, flg, vpr] ← ␈↓
␈↓ ↓H␈↓132␈↓ ¬qChapter VII␈↓ H
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ mkjrst l2␈↓
␈↓ ↓H␈↓4.22)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ combool[␈↓αa|␈↓↓u, m, l2, ¬flg, vpr]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ <combool[␈↓αa|␈↓↓u, m, l, flg, vpr], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ compandor1[␈↓αd|␈↓↓u, m, l, l2, flg, vpr]>␈↓
␈↓ ↓H␈↓␈↓ εH␈↓ ≠133
␈↓ ↓H␈↓α␈↓ ¬nChapter VIII
␈↓ ↓H␈↓α␈↓ ¬BCOMPUTABILITY
␈↓ ↓H␈↓1. ␈↓αA universal LISP function.␈↓
␈↓ ↓H␈↓ Except␈αfor␈αspeed␈αand␈αmemory␈αsize␈αall␈αpresent␈αday␈αstored␈αprogram␈αcomputers␈αare␈αequivalent
␈↓ ↓H␈↓in␈αwhat␈αcomputations␈αthey␈αcan␈αdo.␈α A␈αprogram␈αwritten␈αfor␈αone␈αcomputer␈αcan␈αbe␈αtranslated␈αto␈αrun
␈↓ ↓H␈↓on␈αanother.␈α Indeed,␈αone␈αcan␈αwrite␈αa␈αsimulator␈αfor␈αone␈αcomputer␈αto␈αrun␈αon␈αanother.␈α To␈αput␈αit␈αin
␈↓ ↓H␈↓commercial␈α
terms,␈α
no␈α
computer␈α
manufacturer␈α
can␈α
advertise␈α
that␈α
his␈α
machine␈α
can␈α
do␈αcalculations
␈↓ ↓H␈↓impossible on the machine made by his competitors.
␈↓ ↓H␈↓ This␈αis␈αwell␈αknown␈αintuitively,␈αand␈αthe␈αfirst␈αmathematical␈αtheorem␈αof␈αthis␈αkind␈αwas␈αproved
␈↓ ↓H␈↓by␈αA.M.␈αTuring␈α(1936),␈αwho␈αdefined␈αa␈αprimitive␈αkind␈αof␈αcomputer␈αnow␈αcalled␈αa␈αTuring␈αmachine,
␈↓ ↓H␈↓and␈α⊃showed␈α⊃how␈α⊃to␈α⊃make␈α⊂a␈α⊃universal␈α⊃machine␈α⊃that␈α⊃could␈α⊂do␈α⊃any␈α⊃computation␈α⊃done␈α⊃by␈α⊂any
␈↓ ↓H␈↓Turing␈αmachine␈αwhen␈αgiven␈αa␈αdescription␈αof␈αthe␈αmachine␈αto␈αbe␈αsimulated␈αand␈αthe␈αinitial␈αtape␈αof
␈↓ ↓H␈↓the computation to be imitated.
␈↓ ↓H␈↓ In␈αLISP␈α
the␈αfunction␈α
␈↓↓eval,␈↓␈αdefined␈α
in␈αChapter I␈α
((12.1))␈αis␈α
a␈αuniversal␈α
LISP␈αfunction␈αin␈α
the
␈↓ ↓H␈↓sense␈α
that␈α
any␈α
computation␈α
done␈αby␈α
any␈α
LISP␈α
function␈α
can␈α
be␈αdone␈α
by␈α
␈↓↓eval␈↓␈α
when␈α
␈↓↓eval␈↓␈α
is␈αgiven
␈↓ ↓H␈↓suitable arguments.
␈↓ ↓H␈↓ The␈α⊂way␈α⊂␈↓↓eval␈↓␈α⊂handles␈α⊂arguments␈α∂corresponds␈α⊂to␈α⊂the␈α⊂call-by-value␈α⊂method␈α⊂of␈α∂parameter
␈↓ ↓H␈↓passing␈αin␈αALGOL␈αand␈αsimilar␈αlanguages.␈α There␈αis␈αalso␈αa␈αform␈αof␈α␈↓↓eval␈↓␈αthat␈αcorresponds␈αto␈αcall-
␈↓ ↓H␈↓by-name. Here it is:
␈↓ ↓H␈↓␈↓ α8␈↓↓ neval[e, a] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [␈↓αif␈↓↓ e = ␈↓¬T␈↓↓ ␈↓αthen␈↓↓ ␈↓¬T␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ e = ␈↓¬NIL␈↓↓ ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ numberp e ␈↓αthen␈↓↓ e␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ neval[␈↓αad|␈↓↓assoc[e, a], ␈↓αdd|␈↓↓assoc[e, a]]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓e ␈↓αthen␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ [␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CAR ␈↓↓␈↓αthen␈↓↓ ␈↓αa|␈↓↓neval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CDR ␈↓↓␈↓αthen␈↓↓ ␈↓αd|␈↓↓neval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓1.1)␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CONS ␈↓↓␈↓αthen␈↓↓ neval[␈↓αad|␈↓↓e, a] . neval[␈↓αadd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬ATOM ␈↓↓␈↓αthen␈↓↓ ␈↓αat|␈↓↓neval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬EQ ␈↓↓␈↓αthen␈↓↓ neval[␈↓αad|␈↓↓e, a] = neval[␈↓αadd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬QUOTE ␈↓↓␈↓αthen␈↓↓ ␈↓αad|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬COND ␈↓↓␈↓αthen␈↓↓ nevcon[␈↓αd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬LIST ␈↓↓␈↓αthen␈↓↓ mapcar[␈↓αd|␈↓↓e, λx: neval[x, a]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ neval[␈↓αd|␈↓↓assoc[␈↓αa|␈↓↓e, a] . ␈↓αd|␈↓↓e, a]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓e = ␈↓¬LAMBDA ␈↓↓␈↓αthen␈↓↓ neval[␈↓αadda|␈↓↓e, nprup[␈↓αada|␈↓↓e, ␈↓αd|␈↓↓e, a]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓e = ␈↓¬LABEL ␈↓↓␈↓αthen␈↓↓ neval[␈↓αadda|␈↓↓e . ␈↓αd|␈↓↓e, [␈↓αada|␈↓↓e . ␈↓αa|␈↓↓e] . a]␈↓,
␈↓ ↓H␈↓where the auxiliary function ␈↓↓nevcon␈↓ is given by
␈↓ ↓H␈↓134␈↓ ¬lChapter VIII␈↓ H
␈↓ ↓H␈↓1.2)␈↓ αp␈↓↓nevcon[u, a] ← ␈↓αif␈↓↓ neval[␈↓αaa|␈↓↓u, a] ␈↓αthen␈↓↓ neval[␈↓αada|␈↓↓u, a] ␈↓αelse␈↓↓ nevcon[␈↓αd|␈↓↓u, a]␈↓.
␈↓ ↓H␈↓and ␈↓↓nprup␈↓ is
␈↓ ↓H␈↓1.3)␈↓ αy␈↓↓nprup[u, v,a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ [␈↓αa|␈↓↓u . [␈↓αa|␈↓↓v . a]] . nprup[␈↓αd|␈↓↓u, ␈↓αd|␈↓↓v,a]␈↓.
␈↓ ↓H␈↓ The␈α
difference␈α
between␈α∞␈↓↓eval␈↓␈α
and␈α
␈↓↓neval␈↓␈α
is␈α∞only␈α
in␈α
two␈α
terms.␈α∞ ␈↓↓eval␈↓␈α
evaluates␈α
a␈α∞variable␈α
by
␈↓ ↓H␈↓looking␈αit␈αup␈αon␈αthe␈αassociation␈αlist␈αwhereas␈α
␈↓↓neval␈↓␈αlooks␈αit␈αup␈αon␈αthe␈αassociation␈αlist␈αand␈α
evaluates
␈↓ ↓H␈↓the␈αresult␈αin␈αthe␈αcontext␈αin␈αwhich␈αit␈αwas␈αput␈αon␈αthe␈αassociation␈αlist.␈α Correspondingly,␈αwhen␈αa␈αλ-
␈↓ ↓H␈↓expression␈α
is␈αencountered,␈α
␈↓↓eval␈↓␈α
forms␈αa␈α
new␈α
association␈αlist␈α
by␈α
pairing␈αthe␈α
values␈α
of␈αthe␈α
arguments
␈↓ ↓H␈↓with␈αthe␈α
variables␈αbound␈αby␈α
the␈αλ␈αand␈α
putting␈αthe␈αnew␈α
pairs␈αin␈αfront␈α
of␈αthe␈αold␈α
association␈αlist,
␈↓ ↓H␈↓whereas␈α
␈↓↓neval␈↓␈α
pairs␈α
the␈α
arguments␈α
themselves␈α
with␈αthe␈α
variables␈α
and␈α
puts␈α
them␈α
on␈α
the␈α
front␈αof
␈↓ ↓H␈↓the␈α
association␈αlist.␈α
The␈α
function␈α␈↓↓neval␈↓␈α
also␈α
saves␈αthe␈α
current␈α
association␈αlist␈α
with␈α
each␈αvariable
␈↓ ↓H␈↓on␈αthe␈αassociation␈αlist,␈αso␈αthat␈αthe␈αvariables␈αcan␈αbe␈αevaluated␈αin␈αthe␈αcorrect␈αcontext.␈α In␈αmost␈αcases
␈↓ ↓H␈↓they␈α⊂give␈α∂the␈α⊂same␈α∂result␈α⊂with␈α∂the␈α⊂same␈α∂work,␈α⊂but␈α∂␈↓↓neval␈↓␈α⊂gives␈α∂a␈α⊂result␈α∂for␈α⊂some␈α⊂functions␈α∂of
␈↓ ↓H␈↓several␈αarguments␈αfor␈αwhich␈α␈↓↓eval␈↓␈αloops.␈α An␈αexample␈αis␈αobtained␈αby␈αevaluating␈α␈↓↓F[2,␈α1]␈↓␈αwhere␈α␈↓↓F␈↓
␈↓ ↓H␈↓is defined by
␈↓ ↓H␈↓␈↓ ∧
␈↓↓F[x, y] ← ␈↓αif␈↓↓ x=0 ␈↓αthen␈↓↓ 0 ␈↓αelse␈↓↓ F[x-1, F[y-2, x]]␈↓.
␈↓ ↓H␈↓ Vuillemin␈α
[1973]␈α
showed␈α
that␈α
if␈α
a␈α
function␈α
is␈α
␈↓↓strict,␈↓␈α
never␈α
has␈α
a␈α
defined␈α
value␈α
unless␈α
all
␈↓ ↓H␈↓arguments have a defined value, then call-by-name and call-by-value always give the same result.
␈↓ ↓H␈↓α␈↓ ε
Exercises
␈↓ ↓H␈↓1.␈α⊂Write␈α⊃␈↓↓neval␈↓␈α⊂and␈α⊂the␈α⊃necessary␈α⊂auxiliary␈α⊃functions␈α⊂in␈α⊂list␈α⊃form,␈α⊂and␈α⊂try␈α⊃them␈α⊂out␈α⊃on␈α⊂some
␈↓ ↓H␈↓examples.
␈↓ ↓H␈↓2. ␈↓αNon-computability.␈↓
␈↓ ↓H␈↓ Some␈α∞LISP␈α
calculations␈α∞run␈α∞on␈α
indefinitely.␈α∞ The␈α∞most␈α
trivial␈α∞case␈α∞occurs␈α
if␈α∞we␈α∞make␈α
the
␈↓ ↓H␈↓recursive definition
␈↓ ↓H␈↓2.1)␈↓ ¬(␈↓↓loop x ← loop x ␈↓
␈↓ ↓H␈↓and␈α
attempt␈α
to␈α
compute␈α
␈↓↓loop[x]␈↓␈α
for␈α
any␈α
␈↓↓x␈↓␈α
whatsoever.␈α
Don't␈α
dismiss␈α
this␈α
example␈α
just␈αbecause␈α
no-
␈↓ ↓H␈↓one␈αwould␈αwrite␈αsuch␈αan␈αobviously␈αuseless␈αfunction␈αdefinition.␈α There␈αis␈αa␈αsense␈αin␈αwhich␈αit␈αis␈α
the
␈↓ ↓H␈↓"zero"␈α∞of␈α∞a␈α
large␈α∞class␈α∞of␈α
non-terminating␈α∞function␈α∞definitions,␈α
and,␈α∞as␈α∞the␈α∞Romans␈α
experienced
␈↓ ↓H␈↓but never learned, leaving zero out of the number system is a mistake.
␈↓ ↓H␈↓ Nevertheless,␈αin␈αmost␈α
programming,␈αnon-terminating␈αcalculations␈αare␈α
the␈αresults␈αof␈αerrors␈α
in
␈↓ ↓H␈↓defining␈αfunctions.␈α Therefore,␈αit␈αwould␈αbe␈αuseful␈αto␈αbe␈αable␈αto␈αtell␈αwhether␈αa␈αfunction␈αdefinition
␈↓ ↓H␈↓gives␈αa␈αresult␈α
for␈αall␈αarguments.␈α In␈α
fact,␈αit␈αwould␈αbe␈α
useful␈αto␈αbe␈αable␈α
to␈αtell␈αwhether␈α
a␈αfunction
␈↓ ↓H␈↓will terminate for a single argument. Let us make this goal more precise.
␈↓ ↓H␈↓ Suppose␈α∞that␈α
␈↓↓f␈↓␈α∞is␈α∞a␈α
LISP␈α∞function␈α∞and␈α
␈↓↓a␈↓␈α∞is␈α∞an␈α
S-expression,␈α∞and␈α∞we␈α
would␈α∞like␈α∞to␈α
know
␈↓ ↓H␈↓␈↓ ¬lChapter VIII␈↓ ≠135
␈↓ ↓H␈↓whether␈α
the␈α
computation␈αof␈α
␈↓↓f[a]␈↓␈α
terminates.␈α
Suppose␈α␈↓↓f␈↓␈α
is␈α
represented␈αby␈α
the␈α
S-expression␈α
␈↓↓f*␈↓␈αin
␈↓ ↓H␈↓internal␈α
notation.␈α
Then␈α
the␈α
S-expression␈α
␈↓¬(␈↓↓f*␈↓¬␈α(QUOTE␈α
␈↓↓a␈↓¬))␈↓␈α
represents␈α
␈↓↓f[a].␈↓␈α
Define␈α
the␈αfunction␈α
␈↓↓term␈↓
␈↓ ↓H␈↓by␈αgiving␈α␈↓↓term[e]␈↓␈αthe␈αvalue␈α␈↓αtrue␈↓␈αif␈α␈↓↓e␈↓␈αis␈αan␈αS-expression␈αof␈αthe␈αform␈α␈↓¬(␈↓↓f*␈↓¬␈α(QUOTE␈α␈↓↓a␈↓¬))␈↓␈αfor␈αwhich␈α␈↓↓f[a]␈↓
␈↓ ↓H␈↓terminates␈α⊂and␈α⊂␈↓αfalse␈↓␈α⊂otherwise.␈α⊂ We␈α⊂now␈α⊂ask␈α⊃whether␈α⊂␈↓↓term␈↓␈α⊂is␈α⊂a␈α⊂LISP␈α⊂function,␈α⊂i.e.␈α⊂can␈α⊃it␈α⊂be
␈↓ ↓H␈↓constructed␈α⊂from␈α⊂␈↓↓car,␈↓␈α⊂␈↓↓cdr,␈↓␈α⊂␈↓↓cons,␈↓␈α⊂␈↓↓atom,␈↓␈α⊂ and␈α⊂␈↓↓eq␈↓␈α⊂using␈α⊂␈↓↓λ,␈↓␈α⊂␈↓↓label,␈↓␈α⊂and␈α⊃conditional␈α⊂expressions?
␈↓ ↓H␈↓Well,␈αit␈αcan't,␈αas␈αwe␈αshall␈αshortly␈αprove,␈αand␈αthis␈αmeans␈αthat␈αit␈αis␈αnot␈α␈↓↓computable␈↓␈αwhether␈αa␈αLISP
␈↓ ↓H␈↓calculation␈αterminates,␈αsince␈αif␈α␈↓↓term␈↓␈αwere␈αcomputable␈αby␈αany␈αcomputer␈αor␈αin␈αany␈αrecognized␈αsense,
␈↓ ↓H␈↓it could be represented as a LISP function. Here is the proof:
␈↓ ↓H␈↓ Consider the function ␈↓↓terma␈↓ defined from ␈↓↓term␈↓ by
␈↓ ↓H␈↓2.2)␈↓ β9␈↓↓terma u ← ␈↓αif␈↓↓ term list[u, list[␈↓¬QUOTE, ␈↓↓u]] ␈↓αthen␈↓↓ loop u ␈↓αelse␈↓↓ ␈↓αtrue␈↓↓␈↓,
␈↓ ↓H␈↓and␈α∞suppose␈α∞that␈α∞␈↓↓f␈↓␈α∞is␈α∞a␈α∞LISP␈α∞function␈α
and␈α∞that␈α∞␈↓↓f*␈↓␈α∞is␈α∞its␈α∞S-expression␈α∞representation.␈α∞ What␈α
is
␈↓ ↓H␈↓␈↓↓terma␈↓␈α␈↓↓f*␈↓␈α?␈αWell␈α␈↓↓terma␈↓␈α␈↓↓f*␈↓␈αtells␈αus␈αwhether␈αthe␈αcomputation␈αof␈α␈↓↓f[f*]␈↓␈αterminates,␈αand␈αit␈αtells␈αus␈αthis
␈↓ ↓H␈↓by␈α∞going␈α∂into␈α∞a␈α∂loop␈α∞if␈α∂␈↓↓f[f*]␈↓␈α∞terminates␈α∂and␈α∞giving␈α∂␈↓αtrue␈↓␈α∞otherwise.␈α∂ Now␈α∞if␈α∂␈↓↓term␈↓␈α∞were␈α∂a␈α∞LISP
␈↓ ↓H␈↓function,␈α
then␈α
␈↓↓terma␈↓␈α
would␈αalso␈α
be␈α
a␈α
LISP␈αfunction.␈α
Indeed␈α
if␈α
␈↓↓term␈↓␈αwere␈α
represented␈α
by␈α
the␈αS-
␈↓ ↓H␈↓expression ␈↓↓term*,␈↓ then ␈↓↓terma␈↓ would be represented by the S-expression
␈↓ ↓H␈↓2.3)␈↓ α→␈↓↓terma*␈↓ = ␈↓¬(LAMBDA (U) (COND ((␈↓↓term*␈↓¬ (LIST U (LIST (QUOTE QUOTE) U))) (LOOP U)) (T T)))␈↓.
␈↓ ↓H␈↓Now␈α⊂consider␈α∂␈↓↓terma[terma*].␈↓␈α⊂ According␈α∂to␈α⊂the␈α∂definition␈α⊂of␈α∂␈↓↓terma,␈↓␈α⊂this␈α∂will␈α⊂tell␈α⊂us␈α∂whether
␈↓ ↓H␈↓␈↓↓terma[terma*]␈↓␈αis␈αdefined,␈αi.e.␈αit␈αtells␈αabout␈αitself.␈α However,␈αit␈αgives␈αthis␈αanswer␈αin␈αa␈αcontradictory
␈↓ ↓H␈↓way;␈α
namely␈α␈↓↓terma[terma*]␈↓␈α
looping␈α
tells␈αus␈α
that␈α
␈↓↓terma[terma*]␈↓␈αterminates,␈α
and␈α␈↓↓terma[terma*]␈↓␈α
being
␈↓ ↓H␈↓␈↓αtrue␈↓␈α∞tells␈α∞us␈α
that␈α∞␈↓↓terma[terma*]␈↓␈α∞doesn't␈α∞terminate.␈α
This␈α∞contradiction␈α∞tells␈α∞us␈α
that␈α∞␈↓↓term␈↓␈α∞is␈α∞not␈α
a
␈↓ ↓H␈↓LISP␈α∃function,␈α∃and␈α∃there␈α∃is␈α∃no␈α∀general␈α∃procedure␈α∃for␈α∃telling␈α∃whether␈α∃a␈α∃LISP␈α∀calculation
␈↓ ↓H␈↓terminates.
␈↓ ↓H␈↓ The␈α∩above␈α⊃result␈α∩does␈α⊃not␈α∩exclude␈α⊃LISP␈α∩functions␈α⊃that␈α∩tell␈α⊃whether␈α∩LISP␈α⊃calculations
␈↓ ↓H␈↓terminate.␈α⊃ It␈α⊃just␈α⊃excludes␈α∩perfect␈α⊃ones.␈α⊃ Suppose␈α⊃we␈α⊃have␈α∩a␈α⊃function␈α⊃␈↓↓t␈↓␈α⊃that␈α∩sometimes␈α⊃says
␈↓ ↓H␈↓calculations␈α∂terminate,␈α∞sometimes␈α∂says␈α∂they␈α∞don't␈α∂terminate,␈α∂and␈α∞sometimes␈α∂runs␈α∂on␈α∞indefinitely.
␈↓ ↓H␈↓We␈α
shall␈α
further␈α
assume␈α
that␈α
when␈α
␈↓↓t␈↓␈α
gives␈αan␈α
answer␈α
it␈α
is␈α
always␈α
right.␈α
Given␈α
such␈α
a␈αfunction␈α
we
␈↓ ↓H␈↓can␈αimprove␈αit␈α
a␈αbit␈αso␈α
that␈αit␈αwill␈α
always␈αgive␈αthe␈α
right␈αanswer␈αwhen␈α
the␈αcalculation␈αit␈α
is␈αasked
␈↓ ↓H␈↓about␈α∪terminates.␈α∪ This␈α∀is␈α∪done␈α∪by␈α∀mixing␈α∪the␈α∪computation␈α∪of␈α∀␈↓↓t[e]␈↓␈α∪with␈α∪a␈α∀computation␈α∪of
␈↓ ↓H␈↓␈↓↓eval[e, ␈↓¬NIL␈↓↓]␈↓␈α∞doing␈α∞the␈α∞computations␈α∂alternately.␈α∞ If␈α∞the␈α∞␈↓↓eval[e, ␈↓¬NIL␈↓↓]␈↓␈α∞computation␈α∂ever␈α∞terminates,
␈↓ ↓H␈↓then the new function asserts termination.
␈↓ ↓H␈↓ Given␈αsuch␈αa␈α␈↓↓t,␈↓␈αwe␈αcan␈αalways␈αfind␈αa␈αcalculation␈αthat␈αdoes␈αnot␈αterminate␈αbut␈α␈↓↓t␈↓␈αdoesn't␈αsay
␈↓ ↓H␈↓so. The construction is just like that used in the previous proof. Given ␈↓↓t,␈↓ we construct
␈↓ ↓H␈↓2.4) ␈↓ βg␈↓↓ta u ← ␈↓αif␈↓↓ t list[u, list[␈↓¬QUOTE, ␈↓↓u]] ␈↓αthen␈↓↓ loop u ␈↓αelse␈↓↓ ␈↓αtrue␈↓↓␈↓,
␈↓ ↓H␈↓and␈α
then␈α
we␈α
consider␈α
␈↓↓ta[ta*].␈↓␈α
If␈α
this␈α
had␈α
the␈α
value␈α
␈↓αtrue␈↓,␈α
then␈α
it␈α
wouldn't␈α
terminate␈α
so␈α
therefore␈α
it
␈↓ ↓H␈↓doesn't␈α⊃terminate␈α⊃but␈α⊂is␈α⊃not␈α⊃one␈α⊂of␈α⊃those␈α⊃expressions␈α⊂which␈α⊃␈↓↓t␈↓␈α⊃decides.␈α⊂ Thus␈α⊃for␈α⊃any␈α⊂partial
␈↓ ↓H␈↓decider␈α∂we␈α∂can␈α∞find␈α∂a␈α∂LISP␈α∂calculation␈α∞which␈α∂doesn't␈α∂terminate␈α∞but␈α∂which␈α∂the␈α∂decider␈α∞doesn't
␈↓ ↓H␈↓decide. This can in turn be used to get a slightly better decider, namely
␈↓ ↓H␈↓2.5)␈↓ βt␈↓↓t␈↓β1␈↓↓[e] ← ␈↓αif␈↓↓ e = ta* ␈↓αthen␈↓↓ ␈↓¬DOESN'T-TERMINATE ␈↓↓ ␈↓αelse␈↓↓ t[e]␈↓.
␈↓ ↓H␈↓Of␈αcourse,␈α␈↓↓t␈↓␈↓β1␈↓␈αisn't␈αmuch␈αbetter␈αthan␈α␈↓↓t,␈↓␈αsince␈αit␈αcan␈αdecide␈αonly␈αone␈αmore␈αcomputation,␈αbut␈αwe␈αcan
␈↓ ↓H␈↓136␈↓ ¬lChapter VIII␈↓ H
␈↓ ↓H␈↓form␈α␈↓↓t␈↓␈↓β2␈↓␈αby␈αapplying␈αthe␈αsame␈αprocess,␈αand␈αso␈αforth.␈α In␈αfact,␈αwe␈αcan␈αeven␈αform␈α␈↓↓t␈↓␈↓π␈↓#vw␈↓#␈↓␈αwhich␈αdecides
␈↓ ↓H␈↓all␈αthe␈αcases␈αdecided␈αby␈αany␈α␈↓↓t␈↓␈↓βn␈↓.␈α This␈αcan␈αbe␈αfurther␈αimproved␈αby␈αthe␈αsame␈αprocess,␈αetc.␈α How␈αfar
␈↓ ↓H␈↓can␈α∂we␈α∂go?␈α∞ The␈α∂answer␈α∂is␈α∞technical;␈α∂namely,␈α∂the␈α∞improvement␈α∂process␈α∂can␈α∞be␈α∂carried␈α∂out␈α∞any
␈↓ ↓H␈↓recursive ordinal number of times.
␈↓ ↓H␈↓ Unfortunately,␈α⊂this␈α⊂kind␈α∂of␈α⊂improvement␈α⊂seems␈α∂to␈α⊂be␈α⊂superficial,␈α∂since␈α⊂none␈α⊂of␈α⊂the␈α∂new
␈↓ ↓H␈↓computations proved not to terminate are likely to be of practical interest.
␈↓ ↓H␈↓α␈↓ ε
Exercises
␈↓ ↓H␈↓1. Write a function that gives ␈↓↓t␈↓␈↓βn+1␈↓ in terms of ␈↓↓t␈↓␈↓βn␈↓.
␈↓ ↓H␈↓2. Write a function that gives ␈↓↓t␈↓␈↓π␈↓#vw␈↓#␈↓ in terms of ␈↓↓t.␈↓
␈↓ ↓H␈↓3.␈α∞If␈α∞you␈α∞know␈α∞about␈α
Turing␈α∞machines,␈α∞write␈α∞a␈α∞LISP␈α
function␈α∞to␈α∞simulate␈α∞an␈α∞arbitrary␈α
Turing
␈↓ ↓H␈↓machine given a description of the machine in some convenient notation.
␈↓ ↓H␈↓4.␈α
Write␈α
a␈α
LISP␈α
function␈αthat␈α
will␈α
translate␈α
a␈α
Turing␈αmachine␈α
description␈α
into␈α
a␈α
LISP␈αfunction
␈↓ ↓H␈↓that will do the same computation.
␈↓ ↓H␈↓5.␈αIf␈αyou␈αreally␈αlike␈αTuring␈αmachines,␈αwrite␈αa␈αdescription␈αof␈αa␈αTuring␈αmachine␈αthat␈αwill␈αinterpret
␈↓ ↓H␈↓LISP internal notation.
␈↓ ↓H␈↓␈↓ εH␈↓ ?i
␈↓ ↓H␈↓α␈↓ ¬PBIBLIOGRAPHY
␈↓ ↓H␈↓␈↓αBrudno,␈α∞A.␈α
L.␈α∞[1963]␈↓:␈α∞ "Bounds␈α
and␈α∞Valuations␈α
for␈α∞Shortening␈α∞the␈α
Scanning␈α∞of␈α∞Variations",␈α
[in
␈↓ ↓H␈↓␈↓ αλRussian], ␈↓↓Problemy Kibernetiki␈↓ ␈↓α10␈↓ pp.141-150.
␈↓ ↓H␈↓ First published account of the αβ algorithm.
␈↓ ↓H␈↓␈↓αCartwright,␈α∪Robert␈α∪[1977]␈↓:␈α∪ "Practical␈α∪Formal␈α∪Semantic␈α∪Defintion␈α∪and␈α∪Verification␈α∩Systems,"
␈↓ ↓H␈↓␈↓ αλPh.D. thesis, Computer Science Department, Stanford University, Stanford, Ca..
␈↓ ↓H␈↓␈↓αFilman,␈α⊃R.␈α⊃E.␈α⊃and␈α⊃R.␈α⊃W.␈α⊃Weyhrauch␈α⊃[1976]␈↓:␈α⊃␈↓↓A␈α⊃FOL␈α⊃Primer␈↓,␈α⊃Stanford␈α⊃Artificial␈α⊂Intelligence
␈↓ ↓H␈↓␈↓ αλLaboratory Memo AIM-288.
␈↓ ↓H␈↓ An excellent introduction to using the automatic first order logic proof checker FOL.
␈↓ ↓H␈↓␈↓αGordon,␈α∩M.␈α∩[1975]␈↓:␈α∩ "Operational␈α∩Reasoning␈α∩and␈α∩Denotational␈α∩Semantics",␈α∩Stanford␈α⊃Artificial
␈↓ ↓H␈↓␈↓ αλIntelligence Laboratory Memo, AIM-264.
␈↓ ↓H␈↓␈↓αKnuth,␈αD.␈αE.␈αand␈αR.␈αMoore␈α[1975]␈↓:␈α "An␈αAnalysis␈αof␈αAlpha-Beta␈αPruning",␈α␈↓↓Artificial␈αIntelligence␈↓,
␈↓ ↓H␈↓␈↓ αλ␈↓α6␈↓.
␈↓ ↓H␈↓ A history, description, and analysis of the αβ algorithm.
␈↓ ↓H␈↓␈↓αManna, Zohar [1974]␈↓: ␈↓↓Mathematical Theory of Computation␈↓, McGraw-Hill.
␈↓ ↓H␈↓ Chapter␈α2␈αcontains␈αa␈αdiscussion␈αof␈αfirst␈α
order␈αlogic␈αand␈αof␈αthe␈α␈↓↓Natural deduction␈↓␈αmethod␈α
of
␈↓ ↓H␈↓␈↓ αλproof.
␈↓ ↓H␈↓ Chapter 5 contains a discussion of functionals and fixedpoints.
␈↓ ↓H␈↓ The␈α
book␈αalso␈α
contains␈α
a␈αdiscussion␈α
of␈αstructural␈α
induction␈α
and␈αother␈α
methods␈α
of␈αproving
␈↓ ↓H␈↓␈↓ αλproperties of programs.
␈↓ ↓H␈↓␈↓αMcCarthy,␈α
John␈α[1962a]␈↓:␈α
"Computer␈α
Programs␈αfor␈α
Checking␈αMathematical␈α
Proofs",␈α
␈↓↓Proc.␈αSymp.
␈↓ ↓H␈↓↓␈↓ αλPure Math.␈↓ Vol.5 , Amer. Amth. Soc., Providence, R. I., pp219-227.
␈↓ ↓H␈↓␈↓αMcCarthy,␈α_John␈α↔[1962b]␈↓:␈α_ "Towards␈α↔a␈α_Mathematical␈α↔Science␈α_of␈α↔Computation,"␈α_in␈α_C.␈α↔M.
␈↓ ↓H␈↓␈↓ αλPopplewell␈α
(ed.),␈α
␈↓↓Information␈α
Processing,␈α
Proceedings␈α
of␈α
IFIP␈α
Congress␈α
62␈↓,␈α∞pp21-28,␈α
North
␈↓ ↓H␈↓␈↓ αλHolland Publishing Company, Amsterdam.
␈↓ ↓H␈↓ Contains␈αdiscussions␈αof␈αtransforming␈αFlow␈αChart␈αprograms␈αinto␈αrecursive␈αprograms␈αand␈αof
␈↓ ↓H␈↓␈↓ αλthe Abstract Syntax of programming languages.
␈↓ ↓H␈↓␈↓αMcCarthy,␈αJohn␈α
[1963]␈↓:␈α"A␈αBasis␈α
for␈αa␈αMathematical␈α
Theory␈αof␈αComputation",␈α
in␈αP.␈αBraffort␈α
and
␈↓ ↓H␈↓␈↓ αλD.␈αHirschberg␈α(eds.),␈α␈↓↓Computer␈αProgramming␈αand␈αFormal␈αSystems␈↓,␈αpp.␈α33-70.␈αNorth-Holland
␈↓ ↓H␈↓␈↓ αλPublishing Company, Amsterdam.
␈↓ ↓H␈↓ii␈↓ ¬KBIBLIOGRAPHY␈↓ H
␈↓ ↓H␈↓ Contains␈α⊃a␈α⊃complete␈α⊃discussion␈α⊃of␈α⊃properties␈α⊃of␈α⊃conditional␈α⊃forms,␈α⊃including␈α⊃cannonical
␈↓ ↓H␈↓␈↓ αλforms. Also discusses computable functionals and recursion induction.
␈↓ ↓H␈↓␈↓αMcCarthy,␈α∂John␈α∂[1964]␈↓:␈α∂ "A␈α∞Formal␈α∂Description␈α∂of␈α∂a␈α∂Subset␈α∞of␈α∂Algol",␈α∂␈↓↓Proc.␈α∂Conf.␈α∂on␈α∞Formal
␈↓ ↓H␈↓↓␈↓ αλLanguage Description Languages␈↓, Vienna.
␈↓ ↓H␈↓␈↓αMcCarthy,␈α⊂John␈α⊂[1977]␈↓:␈α⊂ "Representation␈α⊂of␈α⊂Recrusive␈α⊂Programs␈α⊂in␈α⊂First␈α⊂Order␈α⊂Logic,"␈α⊂(to␈α∂be
␈↓ ↓H␈↓␈↓ αλpublished).
␈↓ ↓H␈↓␈↓αMcCarthy,␈α→John␈α_and␈α→James␈α→Painter␈α_[1967]␈↓:␈α→"Correctness␈α→of␈α_a␈α→Compiler␈α→for␈α_Arithmetic
␈↓ ↓H␈↓␈↓ αλExpressions",␈α␈↓↓Proceeding␈αof␈αSymposia␈αin␈αApplied␈α Mathematics␈↓,␈αVol.␈α19,␈αJ.␈αT.␈αSchwartz␈α(ed.),
␈↓ ↓H␈↓␈↓ αλAmerican Mathematical Society, pp 33-41.
␈↓ ↓H␈↓␈↓αMoszkowski, B. [1978]␈↓: Stanford Artificial Intellinence Laboratory Memo, (to appear).
␈↓ ↓H␈↓␈↓αNilsson, N. J. [1971]␈↓: ␈↓↓Problem Solving Methods in Artificial Intelligence␈↓, McGraw-Hill.
␈↓ ↓H␈↓␈↓αPainter,␈α
James␈α
[1967]␈↓:␈α "Semantic␈α
Correctness␈α
of␈αa␈α
Compiler␈α
for␈αan␈α
Algol-like␈α
Language",␈αPh.D.
␈↓ ↓H␈↓␈↓ αλthesis, Computer Science Department, Stanford University, Stanford, Ca..
␈↓ ↓H␈↓␈↓αVuillemin,␈αJ.␈α
[1973]␈↓:␈α"Proof␈α
Techniques␈αfor␈α
Recursive␈αPrograms,"␈α
Ph.D.␈α thesis,␈αComputer␈α
Science
␈↓ ↓H␈↓␈↓ αλDepartment, Stanford University, Stanford, Ca..
␈↓ ↓H␈↓␈↓αWeyhrauch,␈αR.␈αW.␈α[1977]␈↓:␈α "A␈αusers␈αmanual␈αfor␈αFOL",␈αStanford␈αArtificial␈αIntelligence␈α
Laboratory
␈↓ ↓H␈↓␈↓ αλMemo, AIM-235.1.
␈↓ ↓H␈↓ A manual for the automatic proof checker FOL.
␈↓ ↓H␈↓␈↓αWinston, P. H. [1977]␈↓: ␈↓↓Artificial Intelligence␈↓, Addison-Wesley.
␈↓ ↓H␈↓␈↓ εH␈↓ ?i
␈↓ ↓H␈↓α␈↓ ¬uAppendix A
␈↓ ↓H␈↓α␈↓ ∧BExamples of Computer-checked proofs
␈↓ ↓H␈↓ In␈α∞Chapter␈α
III␈α∞we␈α
showed␈α∞how␈α
to␈α∞prove␈α
that␈α∞certain␈α
recursive␈α∞LISP␈α
programs␈α∞meet␈α
their
␈↓ ↓H␈↓specifications.␈α The␈αtechniques␈αpresented␈αin␈αthat␈αchapter␈αrequire␈αfurther␈αdevelopment␈αbefore␈αthey
␈↓ ↓H␈↓can␈αbe␈α
economically␈αapplied␈α
to␈αlarge␈α
programs.␈α One␈α
requirement␈αis␈α
that␈αthe␈α
proofs␈αbe␈αchecked␈α
by
␈↓ ↓H␈↓computer,␈α∞since␈α∞there␈α∞is␈α∞just␈α∞as␈α∞much␈α∞opportunity␈α∞for␈α∞wishful␈α∞thinking␈α∞in␈α∞writing␈α∞or␈α∂reading␈α∞a
␈↓ ↓H␈↓proof of correctness as there is in writing the program in the first place.
␈↓ ↓H␈↓ This␈α∞appendix␈α
contains␈α∞proofs␈α
of␈α∞the␈α∞correctness␈α
of␈α∞␈↓↓samefringe␈↓␈α
in␈α∞a␈α
form␈α∞acceptable␈α∞to␈α
a
␈↓ ↓H␈↓first␈α∀order␈α∀logic␈α∃proof-checker␈α∀called␈α∀FOL␈α∀developed␈α∃at␈α∀the␈α∀Stanford␈α∃University␈α∀Artificial
␈↓ ↓H␈↓Intelligence␈α∩Laboratory.␈α∩ An␈α∩FOL␈α⊃proof␈α∩is␈α∩organized␈α∩into␈α⊃several␈α∩parts.␈α∩ The␈α∩object␈α∩of␈α⊃this
␈↓ ↓H␈↓organization␈α∂is␈α∂first␈α∞to␈α∂establish␈α∂the␈α∞language␈α∂in␈α∂which␈α∞the␈α∂proof␈α∂is␈α∞made,␈α∂then␈α∂to␈α∂present␈α∞the
␈↓ ↓H␈↓general␈αaxioms␈αof␈αthe␈αfield␈αof␈αreasoning␈α(LISP␈αprograms␈αin␈αthe␈αpresent␈αcase),␈αthen␈αto␈αpresent␈αthe
␈↓ ↓H␈↓facts␈α
about␈α
the␈α
particular␈α
problem␈α
(the␈α
function␈α
definitions␈α
in␈α
present␈α
case),␈α
and␈α
finally␈α
to␈αgive␈α
the
␈↓ ↓H␈↓proof␈α
itself.␈α
If␈α
the␈α
stage␈α
has␈α
been␈α
properly␈α
set,␈α
the␈α
proof␈α
itself␈α
will␈α
be␈α
short␈α
and␈αcomprehensible␈α
as
␈↓ ↓H␈↓well as acceptable to the computer.
␈↓ ↓H␈↓1. ␈↓αIntroduction to FOL␈↓
␈↓ ↓H␈↓ We␈α∩give␈α∪here␈α∩a␈α∩very␈α∪brief␈α∩introduction␈α∩to␈α∪FOL␈α∩intended␈α∩only␈α∪to␈α∩help␈α∩the␈α∪reader␈α∩to
␈↓ ↓H␈↓understand␈α⊃the␈α∩"FOL␈α⊃proofs"␈α⊃that␈α∩follow.␈α⊃ The␈α⊃␈↓↓FOL␈↓␈α∩␈↓↓Primer␈↓␈α⊃[Filman␈α⊃and␈α∩Weyhrauch␈α⊃1976]
␈↓ ↓H␈↓provides␈α∂an␈α∞excellent␈α∂introduction␈α∞to␈α∂the␈α∞use␈α∂of␈α∞FOL.␈α∂ A␈α∞more␈α∂detailed␈α∞description␈α∂of␈α∂FOL␈α∞is
␈↓ ↓H␈↓contained in [Weyhrauch 1977].
␈↓ ↓H␈↓ FOL␈αchecks␈αproofs␈αwritten␈αin␈αa␈αnatural␈αdeduction␈αstyle␈αin␈αessentially␈αa␈αfirst␈αorder␈αlanguage.
␈↓ ↓H␈↓The␈α∂language␈α∂is␈α∂specified␈α∂by␈α∂declaring␈α∂all␈α∂of␈α∂the␈α∂variable␈α∂and␈α∂constant␈α∂symbols␈α∂that␈α∂that␈α∂will
␈↓ ↓H␈↓appear.␈α∪ Additional␈α∪information␈α∪may␈α∪be␈α∪included␈α∪in␈α∪the␈α∪declarations␈α∪such␈α∪as␈α∪the␈α∀range␈α∪of
␈↓ ↓H␈↓induvidual variables and the domain and range of operators. Thus the commands
␈↓ ↓H␈↓ ␈↓¬DECLARE PREDCONST ISSEXP 1 [PRE];␈↓
␈↓ ↓H␈↓ ␈↓¬DECLARE INDVAR X Y Z ε ISSEXP;␈↓
␈↓ ↓H␈↓tell␈αFOL␈αthat␈α␈↓¬ISSEXP␈α␈↓is␈αa␈αpredicate␈αwith␈αone␈αargument␈αused␈αas␈αa␈αprefix,␈αand␈αthat␈α␈↓¬X,␈α␈↓␈↓¬Y,␈α␈↓and␈α␈↓¬Z␈α␈↓are
␈↓ ↓H␈↓individual␈α⊗variables␈α⊗ranging␈α⊗over␈α⊗the␈α∃domain␈α⊗with␈α⊗characteristic␈α⊗predicate␈α⊗␈↓¬ISSEXP.␈α⊗␈↓␈α∃This
␈↓ ↓H␈↓declaration also implicitly entails such axioms as ␈↓¬∀X: ISSEXP X␈↓. The command
␈↓ ↓H␈↓ ␈↓¬DECLARE OPCONST CONS (ISSEXP,ISSEXP) = ISSEXP;␈↓
␈↓ ↓H␈↓tells␈αFOL␈αthat␈α␈↓¬CONS␈α␈↓is␈αa␈αfunction␈αtaking␈αtwo␈αarguments.␈α Both␈αmust␈αsatisfy␈α␈↓¬ISSEXP␈αand␈α␈↓the␈αaxiom
␈↓ ↓H␈↓␈↓¬∀X Y: ISSEXP CONS(X,Y)␈↓␈αis␈αimplied.␈α
The␈αextra␈αinformation␈αabout␈α
domains␈αand␈αranges␈αcontained␈α
in
␈↓ ↓H␈↓the␈α∞declarations␈α∞is␈α∞part␈α∂of␈α∞the␈α∞"sorting"␈α∞mechanism␈α∞of␈α∂FOL␈α∞and␈α∞enables␈α∞a␈α∞certain␈α∂ammount␈α∞of
␈↓ ↓H␈↓automatic checking to be carried out when expressions are substituted for "sorted" variables.
␈↓ ↓H␈↓ The general axioms and problem specific facts are given by commands of the form
␈↓ ↓H␈↓ii␈↓ ¬pAppendix A␈↓ H
␈↓ ↓H␈↓ ␈↓¬AXIOM <axiom name>: <wff>;;␈↓
␈↓ ↓H␈↓where ␈↓¬<wff> ␈↓is any sentence of the language which you wish to take as an axiom.
␈↓ ↓H␈↓ The␈α⊂actual␈α⊂proof␈α⊂consists␈α⊂of␈α⊂a␈α⊃sequence␈α⊂of␈α⊂commands␈α⊂intended␈α⊂to␈α⊂generate␈α⊃proof␈α⊂steps.
␈↓ ↓H␈↓When␈αFOL␈αaccepts␈αa␈αcommand␈αand␈αgenerates␈αa␈αnew␈αproof␈αstep,␈αit␈αreplies␈αgiving␈αthe␈αline number
␈↓ ↓H␈↓assigned␈αto␈α
that␈αstep,␈αthe␈α
sentence␈αproved␈αby␈α
the␈αstep␈αand␈α
a␈αlist␈αof␈α
line numbers␈αof␈αassumptions,␈α
if
␈↓ ↓H␈↓any,␈α∂upon␈α∂which␈α∂the␈α∂proof␈α∂step␈α∂depends.␈α∂ The␈α∂rules␈α∂of␈α∂inference␈α∂of␈α∂FOL␈α∂which␈α∂allow␈α∂you␈α∂to
␈↓ ↓H␈↓generate a new proof step from those previously generated are of several types.
␈↓ ↓H␈↓ First␈α∞are␈α∂the␈α∞rules␈α∂of␈α∞natural␈α∂deduction␈α∞consisting␈α∂of␈α∞an␈α∂introduction␈α∞and␈α∂an␈α∞elimination
␈↓ ↓H␈↓rule for each of the locigal connectives and quantifiers. For example
␈↓ ↓H␈↓ ␈↓¬⊃E <step1> <step2>;␈↓
␈↓ ↓H␈↓generates␈α⊃the␈α⊃step␈α⊂proving␈α⊃␈↓¬<wff2>␈α⊃␈↓using␈α⊂the␈α⊃⊃ elimination␈α⊃rule␈α⊂if␈α⊃␈↓¬<step1>␈α⊃␈↓proves␈α⊃␈↓¬<wff1>␈α⊂␈↓and
␈↓ ↓H␈↓␈↓¬<step2> ␈↓proves ␈↓¬<wff1> ⊃ <wff2>␈↓.
␈↓ ↓H␈↓ ␈↓¬∀I <step> <var>;␈↓
␈↓ ↓H␈↓generates the step ␈↓¬∀<var>: <wff>␈↓ where ␈↓¬<step> ␈↓proves ␈↓¬<wff> ␈↓using the ∀ introduction rule.
␈↓ ↓H␈↓ Next␈α⊃there␈α⊃are␈α∩rules␈α⊃which␈α⊃correspond␈α⊃to␈α∩decision␈α⊃procedures␈α⊃for␈α⊃special␈α∩cases.␈α⊃ These
␈↓ ↓H␈↓include␈α∞␈↓¬TAUT␈α∂␈↓␈↓¬(TAUTEQ)␈α∞␈↓for␈α∞deductions␈α∂using␈α∞only␈α∞rules␈α∂of␈α∞propositional␈α∞calculus␈α∂(with␈α∞equality)
␈↓ ↓H␈↓and␈α∃␈↓¬MONADIC␈α∀␈↓for␈α∃deductions␈α∀needing␈α∃only␈α∀facts␈α∃about␈α∀unary␈α∃predicates.␈α∀ For␈α∃example␈α∀the
␈↓ ↓H␈↓command
␈↓ ↓H␈↓ ␈↓¬TAUT <wff> <list of steps and axioms>;␈↓
␈↓ ↓H␈↓will␈αproduce␈αthe␈αstep␈αproving␈α␈↓¬<wff>␈α␈↓if␈αthe␈αdecision␈αprocedure␈α␈↓¬TAUT␈α␈↓determines␈αthat␈α␈↓¬<wff>␈α␈↓follows
␈↓ ↓H␈↓from the list of steps and axoms.
␈↓ ↓H␈↓ Finally␈α∞there␈α∞are␈α∞the␈α∞rules␈α∞␈↓¬ASSUME␈α∞␈↓which␈α
allows␈α∞you␈α∞to␈α∞make␈α∞an␈α∞assumption,␈α∞␈↓¬SUBST␈α
␈↓and
␈↓ ↓H␈↓␈↓¬SUBSTR␈α⊂␈↓which␈α⊃allow␈α⊂substitution␈α⊃of␈α⊂a␈α⊃term␈α⊂(sentence)␈α⊃by␈α⊂one␈α⊃that␈α⊂is␈α⊃equal␈α⊂(equivalent)␈α⊃to␈α⊂it,
␈↓ ↓H␈↓␈↓¬REWRITE␈α␈↓which␈αcorresponds␈αto␈αdeduction␈αby␈αa␈αchain␈αof␈αequalities,␈αand␈α␈↓¬SIMPLIFY␈α␈↓which␈αfor␈αpresent
␈↓ ↓H␈↓purposes␈α∃gives␈α∃access␈α∃to␈α∃the␈α∃axioms␈α∃implied␈α∃by␈α∃the␈α∃sorting␈α∃information␈α∃contained␈α⊗in␈α∃the
␈↓ ↓H␈↓declarations.
␈↓ ↓H␈↓ The␈α∂conventions␈α∞used␈α∂in␈α∞FOL␈α∂to␈α∂denote␈α∞sentences␈α∂and␈α∞terms␈α∂of␈α∞the␈α∂language␈α∂and␈α∞proof
␈↓ ↓H␈↓steps␈αare␈αthe␈αfollowing.␈α A␈αproof␈αstep␈αcan␈αbe␈αgiven␈αa␈αname␈αby␈αthe␈αcommand␈α␈↓¬LABEL.␈α␈↓A␈αproof␈αstep
␈↓ ↓H␈↓can␈α
thus␈αbe␈α
be␈α
referred␈αto␈α
by␈α
its␈αline-number,␈α
its␈α
name␈α(if␈α
any)␈α
and␈αalso␈α
by␈α
a␈αstring␈α
of␈α
↑'s␈αwhere␈α
␈↓↓n␈↓
␈↓ ↓H␈↓↑'s␈α
refers␈αto␈α
the␈α␈↓↓n␈↓th␈α
previous␈αline.␈α
In␈αthe␈α
case␈αof␈α
axioms,␈αif␈α
several␈αaxioms␈α
are␈αdefined␈α
in␈αa␈α
group
␈↓ ↓H␈↓with␈αname␈α<axname>␈αthe␈αthe␈α␈↓↓i␈↓th␈αaxiom␈αof␈αthe␈αgroup␈αis␈αreferred␈αto␈αas␈α<axname>i.␈α Sentences␈αand
␈↓ ↓H␈↓terms␈α
of␈α
the␈α
language␈α
can␈αbe␈α
given␈α
explicitly␈α
or␈α
by␈α
referring␈αto␈α
the␈α
<wff>␈α
(or␈α
a␈α
subpart␈αthereof)␈α
of
␈↓ ↓H␈↓a␈α
step␈α
or␈α
axiom.␈α
If␈α
␈↓↓l␈↓␈α
refers␈α
to␈α
a␈α
proof␈α
step␈α
or␈α
an␈α
axiom␈α
then␈α
␈↓↓l:␈↓␈α
refers␈α
to␈α
the␈α
corresponding␈α
<wff>.
␈↓ ↓H␈↓If␈α
the␈α
outermost␈αconnective,␈α
predicate␈α
or␈αoperator␈α
is␈α
␈↓↓k␈↓-ary.␈αThen␈α
␈↓↓l:#i␈↓␈α
for␈α␈↓↓1 ≤ i ≤ k␈↓␈α
refers␈α
to␈αthe
␈↓ ↓H␈↓␈↓↓i␈↓th subpart of the expresstion and ␈↓↓l:#i#j␈↓ refers to the ␈↓↓j␈↓th subpart of the ␈↓↓i␈↓th subpart, etc..
␈↓ ↓H␈↓ The␈α∞format␈α∞used␈α
for␈α∞presenting␈α∞a␈α
proof␈α∞checked␈α∞by␈α
FOL␈α∞is␈α∞as␈α
follows.␈α∞ The␈α∞axioms␈α
and
␈↓ ↓H␈↓declarations␈αare␈αgiven␈αfirst.␈α These␈αare␈αtaken␈αdirectly␈αfrom␈αthe␈αfiles␈αread␈αby␈αFOL.␈α Following␈αthis
␈↓ ↓H␈↓␈↓ ¬pAppendix A␈↓ -iii
␈↓ ↓H␈↓the␈α
command␈αfiles␈α
for␈α
the␈αvarious␈α
portions␈αof␈α
the␈α
proof␈αare␈α
given.␈α Each␈α
file␈α
starts␈αa␈α
new␈αproof␈α
in
␈↓ ↓H␈↓the␈α∂environment␈α∂of␈α∞the␈α∂axioms␈α∂and␈α∞delcarations.␈α∂ Hence␈α∂some␈α∞of␈α∂the␈α∂files␈α∞begin␈α∂with␈α∂a␈α∂list␈α∞of
␈↓ ↓H␈↓additional␈αaxioms␈αcorresponding␈αto␈αpreviously␈α
proved␈αresults␈αwhich␈αare␈αneeded.␈α
The␈αcommands
␈↓ ↓H␈↓for␈α⊂generating␈α⊂proof␈α⊂steps␈α⊂are␈α⊂interspersed␈α⊃with␈α⊂the␈α⊂generated␈α⊂steps.␈α⊂ In␈α⊂general␈α⊂only␈α⊃the␈α⊂key
␈↓ ↓H␈↓generated␈α∃steps␈α∃are␈α∃included␈α∃to␈α∃make␈α∀the␈α∃proof␈α∃read␈α∃more␈α∃like␈α∃the␈α∃informal␈α∀counterpart.
␈↓ ↓H␈↓Commands␈α∀and␈α∀generated␈α∀steps␈α∀are␈α∀in␈α∪different␈α∀fonts␈α∀so␈α∀they␈α∀can␈α∀be␈α∀easily␈α∪distinguished.
␈↓ ↓H␈↓Commands␈α
are␈α
separated␈α
by␈α
";"␈α
and␈α
more␈α
than␈α
one␈α
may␈α
appear␈α
on␈α
a␈α
line.␈α
The␈α
<wff>␈α
argument␈α
to
␈↓ ↓H␈↓the␈α∂higher␈α∂level␈α∞commands␈α∂is␈α∂omitted␈α∞as␈α∂it␈α∂can␈α∞be␈α∂deduced␈α∂from␈α∞the␈α∂generated␈α∂step␈α∞following.
␈↓ ↓H␈↓The␈α
generated␈α
step␈α∞consists␈α
of␈α
the␈α∞line␈α
number␈α
followed␈α
by␈α∞the␈α
<wff>␈α
followed␈α∞by␈α
a␈α
list␈α∞of␈α
line
␈↓ ↓H␈↓numbers␈αof␈α
assumptions␈αon␈α
which␈αthe␈αstep␈α
depends,␈αif␈α
any.␈α If␈αthe␈α
step␈αhas␈α
been␈αlabeled,␈αthe␈α
label
␈↓ ↓H␈↓appears␈αat␈αthe␈αbeginning␈αof␈αthe␈αline␈αand␈αthe␈αlabel␈αcommand␈αis␈αnot␈αshown.␈α Comments␈αappear␈αin
␈↓ ↓H␈↓bold␈α∞face␈α∞print␈α∞and␈α∞serve␈α∞to␈α∞name␈α∞the␈α∞lemma␈α∞or␈α∞theorem␈α∞being␈α∞proved␈α∞as␈α∞well␈α∞as␈α∞providing␈α∞a
␈↓ ↓H␈↓guide to the organization of the proof.
␈↓ ↓H␈↓iv␈↓ ¬pAppendix A␈↓ H
␈↓ ↓H␈↓α␈↓ ∧)FOL Proof of the SAMEFRINGE theorem.
␈↓ ↓H␈↓α␈↓ ∧5First order axioms for S-EXPRESSIONS
␈↓ ↓H␈↓¬DECLARE PREDCONST ISSEXP ISLIST ATOM NULL 1 [PRE];
␈↓ ↓H␈↓¬DECLARE OPCONST CAR CDR 1 [PRE];
␈↓ ↓H␈↓¬DECLARE OPCONST CONS (ISSEXP,ISSEXP) = ISSEXP;
␈↓ ↓H␈↓¬MOREGENERAL ATOM ≥ {NULL};
␈↓ ↓H␈↓¬MOREGENERAL ISLIST ≥ {NULL};
␈↓ ↓H␈↓¬MOREGENERAL ISSEXP ≥ {ISLIST, ATOM, NULL};
␈↓ ↓H␈↓¬DECLARE INDVAR XX YY;
␈↓ ↓H␈↓¬DECLARE INDVAR X X1 Y Y1 ε ISSEXP;
␈↓ ↓H␈↓¬DECLARE INDVAR U V W ε ISLIST;
␈↓ ↓H␈↓¬DECLARE INDCONST NNIL ε NULL;
␈↓ ↓H␈↓¬DECLARE INDCONST BOTTOM;
␈↓ ↓H␈↓¬DECLARE PREDPAR PHI 1;
␈↓ ↓H␈↓¬AXIOM SEXP:
␈↓ ↓H␈↓¬ SORTAX: ∀U. ISSEXP U;
␈↓ ↓H␈↓¬ ATOMDF: ¬ISSEXP BOTTOM,
␈↓ ↓H␈↓¬ ∀U.(NULL U ≡ ATOM U),
␈↓ ↓H␈↓¬ ∀U.(NULL U ≡ U=NNIL);
␈↓ ↓H␈↓¬ CARDEF: ∀X.(¬ATOM X ⊃ ISSEXP CAR X),
␈↓ ↓H␈↓¬ ∀X Y.(X = CAR CONS(X, Y));
␈↓ ↓H␈↓¬ CDRDEF: ∀X.(¬ATOM X ⊃ ISSEXP CDR X),
␈↓ ↓H␈↓¬ ∀X Y.(Y = CDR CONS(X, Y)),
␈↓ ↓H␈↓¬ ∀U.(¬NULL U ⊃ ISLIST CDR U);
␈↓ ↓H␈↓¬ CONSDF: ∀X U.(ISLIST CONS(X, U)),
␈↓ ↓H␈↓¬ ∀X Y.(ISSEXP CONS(X, Y)),
␈↓ ↓H␈↓¬ ∀X Y.(¬ATOM CONS(X, Y)),
␈↓ ↓H␈↓¬ ∀X.(¬ATOM X ⊃ X = CONS(CAR X, CDR X));
␈↓ ↓H␈↓¬ ;;
␈↓ ↓H␈↓¬AXIOM LISTINDUCTION:
␈↓ ↓H␈↓¬ ∀U.(NULL U ⊃ PHI(U)) ∧ ∀U.(¬NULL U ∧ PHI(CDR U) ⊃ PHI(U)) ⊃ ∀U.PHI(U);;
␈↓ ↓H␈↓¬AXIOM SEXPINDUCTION:
␈↓ ↓H␈↓¬ ∀X.(ATOM X ⊃ PHI(X)) ∧ ∀X.(¬ATOM X ∧ PHI(CAR X) ∧ PHI(CDR X) ⊃ PHI(X)) ⊃ ∀X.PHI(X);;
␈↓ ↓H␈↓αAlternate Axioms
␈↓ ↓H␈↓¬AXIOM SEXP_ALT:
␈↓ ↓H␈↓¬ CARCONS: ∀X Y. (CAR CONS(X, Y) = X);
␈↓ ↓H␈↓¬ CDRCONS: ∀X Y. (CDR CONS(X, Y) = Y);
␈↓ ↓H␈↓¬ EQSEXP: ∀X Y. ((¬ATOM X ∧ ¬ATOM Y) ⊃ (CAR X = CAR Y ∧ CDR X = CDR Y ≡ X = Y));
␈↓ ↓H␈↓¬ ;;
␈↓ ↓H␈↓␈↓ ¬pAppendix A␈↓ 7v
␈↓ ↓H␈↓α␈↓ ∧XAxioms for Extended Truth Values
␈↓ ↓H␈↓¬DECLARE PREDCONST ISTV ISETV 1 [PRE];
␈↓ ↓H␈↓¬DECLARE OPCONST AAND OOR EEQ 2 [INF];
␈↓ ↓H␈↓¬DECLARE OPCONST NNOT AATOM 1 [PRE];
␈↓ ↓H␈↓¬MOREGENERAL ISETV ≥ {ISTV};
␈↓ ↓H␈↓¬DECLARE INDCONST T F ε ISTV;
␈↓ ↓H␈↓¬DECLARE INDVAR P Q εISTV;
␈↓ ↓H␈↓¬DECLARE INDVAR PP QQ εISETV;
␈↓ ↓H␈↓¬AXIOM ETV:
␈↓ ↓H␈↓¬ ETVB: ISETV BOTTOM;
␈↓ ↓H␈↓¬ ETVEXT: ∀PP.(PP=T ∨ PP=F ∨ PP=BOTTOM);
␈↓ ↓H␈↓¬ TVEXT: ∀P.(P=T ∨ P=F);
␈↓ ↓H␈↓¬ TISNTF: ¬(T=F);
␈↓ ↓H␈↓¬ TISNTB: ¬(T=BOTTOM);
␈↓ ↓H␈↓¬ FISNTB: ¬(F=BOTTOM);
␈↓ ↓H␈↓¬ NOTDEF: ∀PP.(NNOT PP = IF (PP=BOTTOM) THEN BOTTOM ELSE IF (PP=T) THEN F ELSE T);
␈↓ ↓H␈↓¬ ANDDEF: ∀PP QQ.(PP AAND QQ = IF (PP=BOTTOM) THEN BOTTOM
␈↓ ↓H␈↓¬ ELSE IF (PP=T) THEN QQ ELSE F);
␈↓ ↓H␈↓¬ ORDEF: ∀PP QQ.(PP OOR QQ = IF (PP=BOTTOM) THEN BOTTOM ELSE IF PP=T THEN T ELSE QQ);
␈↓ ↓H␈↓¬ EQUDEF: ∀XX YY.(XX EEQ YY = IF (¬ISSEXP XX ∨ ¬ISSEXP YY) THEN BOTTOM
␈↓ ↓H␈↓¬ ELSE IF (XX = YY) THEN T ELSE F);
␈↓ ↓H␈↓¬ ATDEF: ∀XX.(AATOM XX = IF (¬ISSEXP XX) THEN BOTTOM ELSE IF ATOM XX THEN T ELSE F);
␈↓ ↓H␈↓¬ ;;
␈↓ ↓H␈↓α␈↓ ¬&Axioms for NATNUMS
␈↓ ↓H␈↓¬DECLARE INDVAR K L M N ε NATNUM;
␈↓ ↓H␈↓¬DECLARE PREDCONST < 2 [INF];
␈↓ ↓H␈↓¬DECLARE OPCONST + 2 [INF];
␈↓ ↓H␈↓¬DECLARE PREDPAR PSI 1;
␈↓ ↓H␈↓¬AXIOM NUMB:
␈↓ ↓H␈↓¬ ISTOT_PLUS: ∀M N.NATNUM (M+N);
␈↓ ↓H␈↓¬ ASSOC: ∀L M N.(L+(M+N)=(L+M)+N);
␈↓ ↓H␈↓¬ ORDER_PLUS: ∀K L M N.(((K<L)∧(M<N))⊃(K+M<L+N));
␈↓ ↓H␈↓¬ ORDER: ∀N.N<1+N;
␈↓ ↓H␈↓¬ ;;
␈↓ ↓H␈↓¬AXIOM NUMBINDUCTION: ∀N.(∀M.(M<N⊃PSI(M))⊃PSI(N)) ⊃ ∀N.PSI(N);;
␈↓ ↓H␈↓vi␈↓ ¬pAppendix A␈↓ H
␈↓ ↓H␈↓α␈↓ ∧YFunction and Predicate Definitions
␈↓ ↓H␈↓¬DECLARE OPCONST FRINGE GOPHER SIZE 1 [PRE];
␈↓ ↓H␈↓¬DECLARE OPCONST * 2 [INF];
␈↓ ↓H␈↓¬DECLARE PREDCONST SAMEFRINGE SAME THM 2;
␈↓ ↓H␈↓¬DECLARE OPCONST SAMEFRINGEA SAMEA 2;
␈↓ ↓H␈↓¬DEFINE FUNCTIONS_AND_PREDICATES:
␈↓ ↓H␈↓¬ APPENDDEF:
␈↓ ↓H␈↓¬ ∀U V.(U * V = IF (NULL U) THEN V ELSE CONS(CAR U, (CDR U) * V));
␈↓ ↓H␈↓¬ SIZEDEF:
␈↓ ↓H␈↓¬ ∀X.(SIZE X =IF ATOM X THEN 1 ELSE (SIZE CAR X)+(SIZE CDR X));
␈↓ ↓H␈↓¬ FRINGEDEF:
␈↓ ↓H␈↓¬ ∀X.(FRINGE X = IF (ATOM X) THEN CONS(X,NNIL) ELSE (FRINGE CAR X) * (FRINGE CDR X));
␈↓ ↓H␈↓¬ SAMEFRINGEDEF:
␈↓ ↓H␈↓¬ ∀X Y.(SAMEFRINGE(X,Y) ≡ (SAMEFRINGEA(X,Y) = T));
␈↓ ↓H␈↓¬ SAMEFRINGEADEF:
␈↓ ↓H␈↓¬ ∀X Y.(SAMEFRINGEA(X,Y) = (X EEQ Y) OOR (((NNOT AATOM X) AAND (NNOT AATOM Y))
␈↓ ↓H␈↓¬ AAND SAMEA(GOPHER X, GOPHER Y)));
␈↓ ↓H␈↓¬ SAMEDEF:
␈↓ ↓H␈↓¬ ∀X Y.(SAME(X,Y) ≡ (SAMEA(X,Y) = T));
␈↓ ↓H␈↓¬ SAMEADEF:
␈↓ ↓H␈↓¬ ∀X Y.(SAMEA(X,Y) = ((CAR X) EEQ (CAR Y)) AAND SAMEFRINGEA(CDR X,CDR Y));
␈↓ ↓H␈↓¬ ETVSAMEA:
␈↓ ↓H␈↓¬ ∀XX YY. ISETV SAMEA(XX,YY);
␈↓ ↓H␈↓¬ GOPHERDEF:
␈↓ ↓H␈↓¬ ∀X.(GOPHER X = IF (ATOM CAR X) THEN X
␈↓ ↓H␈↓¬ ELSE GOPHER CONS(CAR CAR X,CONS(CDR CAR X, CDR X)));
␈↓ ↓H␈↓¬ THMDEF:
␈↓ ↓H␈↓¬ ∀X Y.(THM(X,Y)≡(ISTV SAMEFRINGEA(X,Y))∧
␈↓ ↓H␈↓¬ (SAMEFRINGE(X,Y)≡X=Y∨((¬ATOM X∧¬ATOM Y)∧SAME(GOPHER X,GOPHER Y)))∧
␈↓ ↓H␈↓¬ (SAMEFRINGE(X,Y)≡FRINGE X=FRINGE Y));
␈↓ ↓H␈↓¬ ;;
␈↓ ↓H␈↓␈↓ ¬pAppendix A␈↓ %vii
␈↓ ↓H␈↓α␈↓ βvSummary of Lemmas and Theorems to be proved.
␈↓ ↓H␈↓αAPPEND Lemmas
␈↓ ↓H␈↓¬ NIL_APPEND: ∀U.(NNIL*U)=U
␈↓ ↓H␈↓¬ ISTOT_APPEND: ∀U V.ISLIST (U*V)
␈↓ ↓H␈↓¬ NOTNUL_APPEND: ∀U V.((¬NULL U∨¬NULL V)⊃¬NULL (U*V))
␈↓ ↓H␈↓¬ CAR_APPEND: ∀U V.(¬NULL U⊃CAR (U*V)=CAR U)
␈↓ ↓H␈↓¬ CDR_APPEND: ∀U V.(¬NULL U⊃CDR (U*V)=(CDR U*V))
␈↓ ↓H␈↓¬ ASSOC_APPEND: ∀U V W.((U*V)*W)=(U*(V*W))
␈↓ ↓H␈↓αETV Lemmas
␈↓ ↓H␈↓¬ TVEEQ: ∀X Y.(ISTV X EEQ Y)
␈↓ ↓H␈↓¬ EQEEQ: ∀X Y.((X EEQ Y=TT)≡(X=Y))
␈↓ ↓H␈↓¬ TVOOR: ∀P Q.(ISTV(P OOR Q))
␈↓ ↓H␈↓¬ TVAAND: ∀P Q.(ISTV(P AAND Q))
␈↓ ↓H␈↓¬ EQOOR: ∀P Q.((P OOR Q = TT)≡(P=TT ∨ Q=TT))
␈↓ ↓H␈↓¬ EQAAND: ∀P Q.((P AAND Q = TT)≡(P=TT ∧ Q=TT))
␈↓ ↓H␈↓¬ TVNOTATM: ∀X Y.((ISTV (NNOT AATOM X AAND NNOT AATOM Y)))
␈↓ ↓H␈↓¬ EQNOTATM: ∀X Y.((((NNOT AATOM X AAND NNOT AATOM Y)= TT)≡(¬ATOM X∧¬ATOM Y)))
␈↓ ↓H␈↓¬ POORF: ∀P.(P OOR FF=P)
␈↓ ↓H␈↓¬ FAANDQQ: ∀QQ.(FF AAND QQ = FF)
␈↓ ↓H␈↓αFRINGE GOPHER and SIZE Lemmas
␈↓ ↓H␈↓¬ GOOD_GOPHER: ∀X.((¬ATOM X)⊃(ISSEXP GOPHER X∧ISSEXP CAR GOPHER X
␈↓ ↓H␈↓¬ ∧ISSEXP CDR GOPHER X))
␈↓ ↓H␈↓¬ ISTOT_FRINGE: ∀X.(ISLIST FRINGE X∧¬NULL FRINGE X)
␈↓ ↓H␈↓¬ GOOD_FRINGE: ∀X.(¬ATOM FRINGE X∧ISSEXP FRINGE X)
␈↓ ↓H␈↓¬ ISTOT_SIZE: ∀X.NATNUM(SIZE X)
␈↓ ↓H␈↓¬ FRINGE_ATM: ∀X Y.((ATOM X∨ATOM Y)⊃((FRINGE X=FRINGE Y)≡(X=Y)))
␈↓ ↓H␈↓¬ FRINGE_GOPHER: ∀X.((¬ATOM X)⊃(CAR FRINGE X=CAR GOPHER X
␈↓ ↓H␈↓¬ ∧CDR FRINGE X=FRINGE CDR GOPHER X))
␈↓ ↓H␈↓¬ EQU_FRINGE: ∀X Y.(X=Y∨FRINGE X=FRINGE Y≡FRINGE X=FRINGE Y)
␈↓ ↓H␈↓¬ SIZE_GOPHER: ∀X Y.((¬ATOM X∧¬ATOM Y)
␈↓ ↓H␈↓¬ ⊃((SIZE CDR GOPHER X+SIZE CDR GOPHER Y)<(SIZE X+SIZE Y)))
␈↓ ↓H␈↓αSamefringe Theorems
␈↓ ↓H␈↓¬ THM1: ∀X Y. ISTV SAMEFRINGEA(X,Y)
␈↓ ↓H␈↓¬ THM2: ∀X Y.(SAMEFRINGE(X,Y)=(X=Y)∨((¬ATOM X∧¬ATOM Y)∧SAME(GOPHER X,GOPHER Y)))
␈↓ ↓H␈↓¬ THM3: ∀X Y.(SAMEFRINGE(X,Y)≡FRINGE X=FRINGE Y)
␈↓ ↓H␈↓viii␈↓ ¬pAppendix A␈↓ H
␈↓ ↓H␈↓α␈↓ ∧nProof of properties of APPEND
␈↓ ↓H␈↓ε ␈↓↓To␈α∞provide␈α∞a␈α∞better␈α∞introduction␈α∞to␈α∞FOL,␈α∂all␈α∞of␈α∞the␈α∞generated␈α∞proof␈α∞steps␈α∞are␈α∞given␈α∂for␈α∞the
␈↓ ↓H␈↓↓APPEND lemmas.␈↓ε
␈↓ ↓H␈↓α␈↓ ¬)Proving NIL_APPEND
␈↓ ↓H␈↓εSIMPLIFY ;
␈↓ ↓H␈↓ε␈↓¬1: NULL NNIL ␈↓ε
␈↓ ↓H␈↓εMONADIC APPENDDEF,↑;
␈↓ ↓H␈↓ε␈↓¬NIL_APPEND: 2: ∀U.(NNIL*U)=U␈↓ε
␈↓ ↓H␈↓α␈↓ ¬∀Proving ISTOT_APPEND
␈↓ ↓H␈↓ε␈↓α Induction axiom.␈↓ε
␈↓ ↓H␈↓ε∧I LISTINDUCTION [PHI←λU.∀V.ISLIST U*V];
␈↓ ↓H␈↓ε␈↓¬TOTAPP_IND: 3: (∀U.(NULL U⊃∀V.ISLIST (U*V))∧∀U.((¬NULL U∧∀V.ISLIST (CDR U*V))⊃∀V.ISLIST (U*V)))
␈↓ ↓H␈↓¬ ⊃∀U V.ISLIST (U*V)␈↓ε
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬NULL U␈↓ε
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬NULLU: 4: NULL U (4)␈↓ε
␈↓ ↓H␈↓εSIMPLIFY ;
␈↓ ↓H␈↓ε␈↓¬5: ISLIST V ␈↓ε
␈↓ ↓H␈↓εMONADIC APPENDDEF ↑,NULLU;
␈↓ ↓H␈↓ε␈↓¬6: ISLIST (U*V) (4)␈↓ε
␈↓ ↓H␈↓ε∀I ↑ V;
␈↓ ↓H␈↓ε␈↓¬7: ∀V.ISLIST (U*V) (4)␈↓ε
␈↓ ↓H␈↓ε⊃I NULLU⊃↑;
␈↓ ↓H␈↓ε␈↓¬8: NULL U⊃∀V.ISLIST (U*V) ␈↓ε
␈↓ ↓H␈↓ε∀I ↑ U;
␈↓ ↓H␈↓ε␈↓¬TOTAPP_NUL: 9: ∀U.(NULL U⊃∀V.ISLIST (U*V))␈↓ε
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬¬NULL U␈↓ε
␈↓ ↓H␈↓ε␈↓αInduction hypothesis.␈↓ε
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬TOTAPP_HYP: 10: ¬NULL U∧∀V.ISLIST (CDR U*V) (10)␈↓ε
␈↓ ↓H␈↓εMONADIC ↑,CONSDF1,CARDEF1,ATOMDF2;
␈↓ ↓H␈↓ε␈↓¬11: ISLIST CONS(CAR U,CDR U*V) (10)␈↓ε
␈↓ ↓H␈↓εMONADIC APPENDDEF,TOTAPP_HYP,↑;
␈↓ ↓H␈↓ε␈↓¬12: ISLIST (U*V) (10)␈↓ε
␈↓ ↓H␈↓ε∀I ↑ V;
␈↓ ↓H␈↓ε␈↓¬13: ∀V.ISLIST (U*V) (10)␈↓ε
␈↓ ↓H␈↓ε⊃I TOTAPP_HYP⊃↑;
␈↓ ↓H␈↓ε␈↓¬14: (¬NULL U∧∀V.ISLIST (CDR U*V))⊃∀V.ISLIST (U*V) ␈↓ε
␈↓ ↓H␈↓ε∀I ↑ U;
␈↓ ↓H␈↓ε␈↓¬15: ∀U.((¬NULL U∧∀V.ISLIST (CDR U*V))⊃∀V.ISLIST (U*V))␈↓ε
␈↓ ↓H␈↓ε␈↓α Combine cases and apply induction axiom.␈↓ε
␈↓ ↓H␈↓εTAUT TOTAPP_IND,TOTAPP_NUL,↑;
␈↓ ↓H␈↓ε␈↓¬ISTOT_APPEND: 16: ∀U V.ISLIST (U*V)␈↓ε
␈↓ ↓H␈↓␈↓ ¬pAppendix A␈↓ .ix
␈↓ ↓H␈↓α␈↓ ¬αProving NOTNUL_APPEND
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬¬NULL U␈↓ε
␈↓ ↓H␈↓εMONADIC ATOMDF2,CARDEF1,CDRDEF3,CONSDF1,CONSDF3,ISTOT_APPEND;
␈↓ ↓H␈↓ε␈↓¬17: ¬NULL U⊃¬NULL CONS(CAR U,CDR U*V) ␈↓ε
␈↓ ↓H␈↓εMONADIC ↑,APPENDDEF;
␈↓ ↓H␈↓ε␈↓¬18: ¬NULL U⊃¬NULL (U*V) ␈↓ε
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬¬NULL V␈↓ε
␈↓ ↓H␈↓εMONADIC APPENDDEF;
␈↓ ↓H␈↓ε␈↓¬19: (NULL U∧¬NULL V)⊃¬NULL (U*V) ␈↓ε
␈↓ ↓H␈↓εTAUT ↑,↑↑;
␈↓ ↓H␈↓ε␈↓¬20: (¬NULL U∨¬NULL V)⊃¬NULL (U*V) ␈↓ε
␈↓ ↓H␈↓ε∀I ↑ U V;
␈↓ ↓H␈↓ε␈↓¬NOTNUL_APPEND: 21: ∀U V.((¬NULL U∨¬NULL V)⊃¬NULL (U*V))␈↓ε
␈↓ ↓H␈↓α␈↓ ∧↔Proving CAR_APPEND and CDR_APPEND
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬NOTNULU: 22: ¬NULL U (22)␈↓ε
␈↓ ↓H␈↓εMONADIC APPENDDEF,NOTNULU;
␈↓ ↓H␈↓ε␈↓¬APPENDNOTNUL: 23: (U*V)=CONS(CAR U,CDR U*V) (22)␈↓ε
␈↓ ↓H␈↓αCAR part
␈↓ ↓H␈↓ε∀E CARDEF2 CAR U,CDR U*V;
␈↓ ↓H␈↓ε␈↓¬24: ISSEXP CAR U⊃(ISSEXP (CDR U*V)⊃CAR U=CAR CONS(CAR U,CDR U*V)) ␈↓ε
␈↓ ↓H␈↓εMONADIC ↑,NOTNULU CARDEF1,CDRDEF3,ATOMDF2,SORTAX,ISTOT_APPEND;
␈↓ ↓H␈↓ε␈↓¬25: CAR CONS(CAR U,CDR U*V)=CAR U (22)␈↓ε
␈↓ ↓H␈↓εSUBST APPENDNOTNUL IN ↑;
␈↓ ↓H␈↓ε␈↓¬26: CAR (U*V)=CAR U (22)␈↓ε
␈↓ ↓H␈↓ε⊃I NOTNULU⊃↑;
␈↓ ↓H␈↓ε␈↓¬27: ¬NULL U⊃CAR (U*V)=CAR U ␈↓ε
␈↓ ↓H␈↓ε∀I ↑ U V;
␈↓ ↓H␈↓ε␈↓¬CAR_APPEND: 28: ∀U V.(¬NULL U⊃CAR (U*V)=CAR U) ␈↓ε
␈↓ ↓H␈↓αCDR part
␈↓ ↓H␈↓ε∀E CDRDEF2 CAR U,CDR U*V;
␈↓ ↓H␈↓ε␈↓¬29: ISSEXP CAR U⊃(ISSEXP (CDR U*V)⊃(CDR U*V)=CDR CONS(CAR U,CDR U*V)) ␈↓ε
␈↓ ↓H␈↓εMONADIC ↑,NOTNULU,CARDEF1,CDRDEF3,ATOMDF2,SORTAX,ISTOT_APPEND;
␈↓ ↓H␈↓ε␈↓¬30: CDR CONS(CAR U,CDR U*V)=(CDR U*V) (22)␈↓ε
␈↓ ↓H␈↓εSUBST APPENDNOTNUL IN ↑;
␈↓ ↓H␈↓ε␈↓¬31: CDR (U*V)=(CDR U*V) (22)␈↓ε
␈↓ ↓H␈↓ε⊃I NOTNULU⊃↑;
␈↓ ↓H␈↓ε␈↓¬32: ¬NULL U⊃CDR (U*V)=(CDR U*V) ␈↓ε
␈↓ ↓H␈↓ε∀I ↑ U V;
␈↓ ↓H␈↓ε␈↓¬CDR_APPEND: 33: ∀U V.(¬NULL U⊃CDR (U*V)=(CDR U*V)) ␈↓ε
␈↓ ↓H␈↓x␈↓ ¬pAppendix A␈↓ H
␈↓ ↓H␈↓α␈↓ ¬∪Proving ASSOC_APPEND
␈↓ ↓H␈↓αInduction axiom
␈↓ ↓H␈↓ε∧I LISTINDUCTION [PHI←λU.∀V W.((U*V)*W=U*(V*W))];
␈↓ ↓H␈↓ε␈↓¬ASSOC_IND: 34: (∀U.(NULL U⊃∀V W.((U*V)*W)=(U*(V*W)))
␈↓ ↓H␈↓¬ ∧∀U.((¬NULL U∧∀V W.((CDR U*V)*W)=(CDR U*(V*W)))⊃∀V W.((U*V)*W)=(U*(V*W))))
␈↓ ↓H␈↓¬ ⊃∀U V W.((U*V)*W)=(U*(V*W)) ␈↓ε
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬NULL U␈↓ε
␈↓ ↓H␈↓εMONADIC APPENDDEF,NULLU;
␈↓ ↓H␈↓ε␈↓¬ASSOC1: 35: (U*V)=V (4)␈↓ε
␈↓ ↓H␈↓ε∀E APPENDDEF U,V*W;
␈↓ ↓H␈↓ε␈↓¬36: ISLIST (V*W)⊃((NULL U⊃(U*(V*W))=(V*W))
␈↓ ↓H␈↓¬ ∧(¬NULL U⊃(U*(V*W))=CONS(CAR U,CDR U*(V*W)))) ␈↓ε
␈↓ ↓H␈↓εMONADIC ↑,ISTOT_APPEND,NULLU;
␈↓ ↓H␈↓ε␈↓¬ASSOC2: 37: (V*W)=(U*(V*W)) (4)␈↓ε
␈↓ ↓H␈↓εTAUTEQ ;
␈↓ ↓H␈↓ε␈↓¬38: ((U*V)*W)=((U*V)*W) ␈↓ε
␈↓ ↓H␈↓εSUBSTR ASSOC1 IN ↑ OCC 2;
␈↓ ↓H␈↓ε␈↓¬39: ((U*V)*W)=(V*W) (4)␈↓ε
␈↓ ↓H␈↓εSUBSTR ASSOC2 IN ↑;
␈↓ ↓H␈↓ε␈↓¬40: ((U*V)*W)=(U*(V*W)) (4)␈↓ε
␈↓ ↓H␈↓ε∀I ↑ V W;
␈↓ ↓H␈↓ε␈↓¬41: ∀V W.((U*V)*W)=(U*(V*W)) (4)␈↓ε
␈↓ ↓H␈↓ε⊃I NULLU⊃↑;
␈↓ ↓H␈↓ε␈↓¬42: NULL U⊃∀V W.((U*V)*W)=(U*(V*W)) ␈↓ε
␈↓ ↓H␈↓ε∀I ↑ U;
␈↓ ↓H␈↓ε␈↓¬ASSOC_NUL: 43: ∀U.(NULL U⊃∀V W.((U*V)*W)=(U*(V*W))) ␈↓ε
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬¬NULL U␈↓ε
␈↓ ↓H␈↓ε␈↓αInduction hypothesis␈↓ε
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬ASSOC_HYP: 44: ¬NULL U∧∀V W.((CDR U*V)*W)=(CDR U*(V*W)) (44)␈↓ε
␈↓ ↓H␈↓αForm a chain of equalities.
␈↓ ↓H␈↓ε∀E APPENDDEF U*V,W;
␈↓ ↓H␈↓ε␈↓¬45: ISLIST (U*V)⊃((NULL (U*V)⊃((U*V)*W)=W)
␈↓ ↓H␈↓¬ ∧(¬NULL (U*V)⊃((U*V)*W)=CONS(CAR (U*V),CDR (U*V)*W))) ␈↓ε
␈↓ ↓H␈↓ε∀E NOTNUL_APPEND U,V;
␈↓ ↓H␈↓ε␈↓¬46: (¬NULL U∨¬NULL V)⊃¬NULL (U*V) ␈↓ε
␈↓ ↓H␈↓εMONADIC ↑↑:↑,ASSOC_HYP,ISTOT_APPEND;
␈↓ ↓H␈↓ε␈↓¬ASSOC3: 47: ((U*V)*W)=CONS(CAR (U*V),CDR (U*V)*W) (44)␈↓ε
␈↓ ↓H␈↓εMONADIC CAR_APPEND,ASSOC_HYP;
␈↓ ↓H␈↓ε␈↓¬ASSOC4: 48: CAR (U*V)=CAR U (44)␈↓ε
␈↓ ↓H␈↓εMONADIC CDR_APPEND,ASSOC_HYP;
␈↓ ↓H␈↓ε␈↓¬ASSOC5: 49: CDR (U*V)=(CDR U*V) (44)␈↓ε
␈↓ ↓H␈↓εMONADIC ASSOC_HYP;
␈↓ ↓H␈↓ε␈↓¬ASSOC6: 50: ((CDR U*V)*W)=(CDR U*(V*W)) (44)␈↓ε
␈↓ ↓H␈↓ε∀E APPENDDEF U,V*W;
␈↓ ↓H␈↓ε␈↓¬51: ISLIST (V*W)⊃((NULL U⊃(U*(V*W))=(V*W))
␈↓ ↓H␈↓¬ ∧(¬NULL U⊃(U*(V*W))=CONS(CAR U,CDR U*(V*W)))) ␈↓ε
␈↓ ↓H␈↓εMONADIC ↑,ASSOC_HYP,ISTOT_APPEND;
␈↓ ↓H␈↓ε␈↓¬ASSOC7: 52: CONS(CAR U,CDR U*(V*W))=(U*(V*W)) (44)␈↓ε
␈↓ ↓H␈↓␈↓ ¬pAppendix A␈↓ .xi
␈↓ ↓H␈↓ε␈↓αTransform␈↓ε ␈↓¬(U*V)*W␈↓ε ␈↓αinto␈↓ε ␈↓¬U*(V*W)␈↓ε ␈↓αusing the equalities.␈↓ε
␈↓ ↓H␈↓εREWRITE (U*V)*W BY {ASSOC3,ASSOC4,ASSOC5,ASSOC6,ASSOC7};
␈↓ ↓H␈↓ε␈↓¬53: ((U*V)*W)=(U*(V*W)) (44)␈↓ε
␈↓ ↓H␈↓ε∀I ↑ V W;
␈↓ ↓H␈↓ε␈↓¬54: ∀V W.((U*V)*W)=(U*(V*W)) (44)␈↓ε
␈↓ ↓H␈↓ε⊃I ASSOC_HYP⊃↑;
␈↓ ↓H␈↓ε␈↓¬55: (¬NULL U∧∀V W.((CDR U*V)*W)=(CDR U*(V*W)))⊃∀V W.((U*V)*W)=(U*(V*W)) ␈↓ε
␈↓ ↓H␈↓ε∀I ↑ U;
␈↓ ↓H␈↓ε␈↓¬56: ∀U.((¬NULL U∧∀V W.((CDR U*V)*W)=(CDR U*(V*W)))⊃∀V W.((U*V)*W)=(U*(V*W))) ␈↓ε
␈↓ ↓H␈↓αCombine cases and appply induction axiom.
␈↓ ↓H␈↓εTAUT ASSOC_IND,ASSOC_NUL,↑;
␈↓ ↓H␈↓ε␈↓¬ASSOC_APPEND: 57: ∀U V W.((U*V)*W)=(U*(V*W)) ␈↓ε
␈↓ ↓H␈↓xii␈↓ ¬pAppendix A␈↓ H
␈↓ ↓H␈↓α␈↓ ¬oETV lemmas
␈↓ ↓H␈↓εSIMPLIFY ;
␈↓ ↓H␈↓ε␈↓¬TVNESS: 1: ISTV TT∧(ISTV FF∧ISTV Q) ␈↓ε
␈↓ ↓H␈↓α␈↓ ¬↓Proving TVEEQ and EQEEQ
␈↓ ↓H␈↓ε∀E EQUDEF X Y; SIMPLIFY ;
␈↓ ↓H␈↓ε␈↓¬3: (X EEQ Y)=IF X=Y THEN TT ELSE FF ␈↓ε
␈↓ ↓H␈↓εTAUTEQ ISTV (X EEQ Y) ↑,TVNESS; ∀I ↑ X Y;
␈↓ ↓H␈↓ε␈↓¬TVEEQ: 5: ∀X Y.ISTV (X EEQ Y) ␈↓ε
␈↓ ↓H␈↓εTAUTEQ (X EEQ Y) =TT ≡ X = Y ↑↑↑,TVSET; ∀I ↑ X Y;
␈↓ ↓H␈↓ε␈↓¬EQEEQ: 7: ∀X Y.((X EEQ Y)=TT≡X=Y) ␈↓ε
␈↓ ↓H␈↓α␈↓ ∧eProving TVAAND and EQAAND
␈↓ ↓H␈↓εMONADIC ANDDEF,TVEXT,TVSET;
␈↓ ↓H␈↓ε␈↓¬8: (P AAND Q)=IF P=TT THEN Q ELSE FF ␈↓ε
␈↓ ↓H␈↓εTAUTEQ ISTV (P AAND Q) ↑,TVNESS; ∀I ↑ P Q;
␈↓ ↓H␈↓ε␈↓¬TVAAND: 10: ∀P Q.ISTV (P AAND Q) ␈↓ε
␈↓ ↓H␈↓εTAUTEQ (P AAND Q) = TT ≡ P=TT ∧ Q=TT ↑↑↑,TVSET; ∀I ↑ P Q;
␈↓ ↓H␈↓ε␈↓¬EQAAND: 12: ∀P Q.((P AAND Q)=TT≡(P=TT∧Q=TT)) ␈↓ε
␈↓ ↓H␈↓α␈↓ ∧zProving TVOOR and EQOOR
␈↓ ↓H␈↓εMONADIC ORDEF,TVEXT,TVSET;
␈↓ ↓H␈↓ε␈↓¬13: (P OOR Q)=IF P=TT THEN TT ELSE Q ␈↓ε
␈↓ ↓H␈↓εTAUTEQ ISTV (P OOR Q) ↑,TVNESS; ∀I ↑ P Q;
␈↓ ↓H␈↓ε␈↓¬TVOOR: 15: ∀P Q.ISTV (P OOR Q) ␈↓ε
␈↓ ↓H␈↓εTAUTEQ (P OOR Q) = TT ≡ P=TT ∨ Q=TT ↑↑↑,TVSET; ∀I ↑ P Q;
␈↓ ↓H␈↓ε␈↓¬EQOOR: 17: ∀P Q.((P OOR Q)=TT≡(P=TT∨Q=TT)) ␈↓ε
␈↓ ↓H␈↓α␈↓ ∧4Proving TVNOTATM and EQNOTATM
␈↓ ↓H␈↓ε∀E ATDEF X; SIMPLIFY ;
␈↓ ↓H␈↓ε␈↓¬AATOMX: 19: AATOM X=IF ATOM X THEN TT ELSE FF ␈↓ε
␈↓ ↓H␈↓εMONADIC NOTDEF,TVEXT,TVSET;
␈↓ ↓H␈↓ε␈↓¬NNOTP: 20: NNOT P=IF P=TT THEN FF ELSE TT ␈↓ε
␈↓ ↓H␈↓α␈↓ β:Proving TVNNOT TVAATOM EQNNOT and EQAATOM
␈↓ ↓H␈↓εTAUTEQ ISTV NNOT P NNOTP,TVNESS; ∀I ↑ P;
␈↓ ↓H␈↓ε␈↓¬TVNNOT: 22: ∀P.ISTV NNOT P ␈↓ε
␈↓ ↓H␈↓εTAUTEQ NNOT P = TT ≡ ¬(P = TT) NNOTP,TVSET; ∀I ↑ P;
␈↓ ↓H␈↓ε␈↓¬EQNNOT: 24: ∀P.(NNOT P=TT≡¬(P=TT)) ␈↓ε
␈↓ ↓H␈↓εTAUTEQ ISTV AATOM X AATOMX,TVNESS; ∀I ↑ X;
␈↓ ↓H␈↓ε␈↓¬TVAATOM: 26: ∀X.ISTV AATOM X ␈↓ε
␈↓ ↓H␈↓εTAUTEQ AATOM X = TT ≡ ATOM X AATOMX,TVSET; ∀I ↑ X;
␈↓ ↓H␈↓ε␈↓¬EQAATOM: 28: ∀X.(AATOM X=TT≡ATOM X) ␈↓ε
␈↓ ↓H␈↓␈↓ ¬pAppendix A␈↓ ≤xiii
␈↓ ↓H␈↓α␈↓ ∧4Proving TVNOTATM and EQNOTATM
␈↓ ↓H␈↓εMONADIC TVAATOM,TVNNOT;
␈↓ ↓H␈↓ε␈↓¬29: ∀X.ISTV NNOT AATOM X ␈↓ε
␈↓ ↓H␈↓εMONADIC TVAAND,↑;
␈↓ ↓H␈↓ε␈↓¬TVNOTATM: 30: ∀X Y.ISTV (NNOT AATOM X AAND NNOT AATOM Y) ␈↓ε
␈↓ ↓H␈↓εMONADIC EQNNOT, EQAATOM, TVAATOM;
␈↓ ↓H␈↓ε␈↓¬31: ∀X.(NNOT AATOM X=TT≡¬ATOM X) ␈↓ε
␈↓ ↓H␈↓ε∀E EQAAND NNOT AATOM X, NNOT AATOM Y;
␈↓ ↓H␈↓εMONADIC ↑,↑↑,↑↑↑↑;
␈↓ ↓H␈↓ε␈↓¬33: (NNOT AATOM X AAND NNOT AATOM Y)=TT≡(¬ATOM X∧¬ATOM Y) ␈↓ε
␈↓ ↓H␈↓ε∀I ↑ X Y;
␈↓ ↓H␈↓ε␈↓¬EQNOTATM: 34: ∀X Y.((NNOT AATOM X AAND NNOT AATOM Y)=TT≡(¬ATOM X∧¬ATOM Y)) ␈↓ε
␈↓ ↓H␈↓α␈↓ ∧gProving POORF and FAANDQQ
␈↓ ↓H␈↓εMONADIC ORDEF,TVEXT,TVSET;
␈↓ ↓H␈↓ε␈↓¬POORF: 35: ∀P.(P OOR FF)=P ␈↓ε
␈↓ ↓H␈↓εMONADIC ANDDEF,TVSET;
␈↓ ↓H␈↓ε␈↓¬FAANDQQ: 36: ∀QQ.(FF AAND QQ)=FF ␈↓ε
␈↓ ↓H␈↓xiv␈↓ ¬pAppendix A␈↓ H
␈↓ ↓H␈↓α␈↓ ∧↔Lemmas about GOPHER, FRINGE and SIZE
␈↓ ↓H␈↓αPreviously proved results
␈↓ ↓H␈↓ε␈↓¬AXIOM ISTOT_APPEND: ∀U V.ISLIST(U*V);;
␈↓ ↓H␈↓¬AXIOM NOTNUL_APPEND: ∀U V.((¬NULL U∨¬NULL V)⊃¬NULL(U*V));;
␈↓ ↓H␈↓¬AXIOM ASSOC_APPEND: ∀U V W.((U*V)*W=U*(V*W));;
␈↓ ↓H␈↓¬AXIOM CDR_APPEND: ∀U V.(¬NULL U⊃CDR (U*V)=(CDR U*V));;
␈↓ ↓H␈↓¬AXIOM CAR_APPEND: ∀U V.(¬NULL U⊃CAR (U*V)=CAR U);;␈↓ε
␈↓ ↓H␈↓αBasic definitions and assumptions
␈↓ ↓H␈↓εSIMPLIFY ;
␈↓ ↓H␈↓ε␈↓¬NULNIL: 1: NULL NNIL ␈↓ε
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬XISATM: 2: ATOM X (2)␈↓ε
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬XISNTATM: 3: ¬ATOM X (3)␈↓ε
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬XISY: 4: X=Y (4)␈↓ε
␈↓ ↓H␈↓εMONADIC XISNTATM,CONSDF4;
␈↓ ↓H␈↓ε␈↓¬MAKEX: 5: CONS(CAR X,CDR X)=X (3)␈↓ε
␈↓ ↓H␈↓α␈↓ ¬⊃Proof of GOOD_GOPHER
␈↓ ↓H␈↓ε␈↓αFirst prove desired properties for␈↓ε ␈↓¬GOPHER CONS(X, Y)␈↓ε
␈↓ ↓H␈↓εREWRITE GOPHER CONS(X,Y) BY {GOPHERDEF,CARCONS,CDRCONS};
␈↓ ↓H␈↓ε␈↓¬GOPHER_CONS: 6: GOPHER CONS(X,Y)=IF ATOM X THEN CONS(X,Y)
␈↓ ↓H␈↓¬ ELSE GOPHER CONS(CAR X,CONS(CDR X,Y))␈↓ε
␈↓ ↓H␈↓εTAUTEQ ↑,XISATM;
␈↓ ↓H␈↓ε␈↓¬GOPHER_CONS_ATM: 7: CONS(X,Y)=GOPHER CONS(X,Y) (2)␈↓ε
␈↓ ↓H␈↓ε␈↓αInduction axiom␈↓ε
␈↓ ↓H␈↓ε∧I SEXPINDUCTION [PHI←λX.∀Y.(ISSEXP GOPHER CONS(X,Y)∧¬ATOM GOPHER CONS(X,Y))];
␈↓ ↓H␈↓ε␈↓¬GOODGO_IND: 8: (∀X.(ATOM X⊃∀Y.(ISSEXP GOPHER CONS(X,Y)∧¬ATOM GOPHER CONS(X,Y)))
␈↓ ↓H␈↓¬ ∧∀X.((¬ATOM X∧(∀Y.(ISSEXP GOPHER CONS(CAR X,Y)∧¬ATOM GOPHER CONS(CAR X,Y))
␈↓ ↓H␈↓¬ ∧∀Y.(ISSEXP GOPHER CONS(CDR X,Y)∧¬ATOM GOPHER CONS(CDR X,Y))))
␈↓ ↓H␈↓¬ ⊃∀Y.(ISSEXP GOPHER CONS(X,Y)∧¬ATOM GOPHER CONS(X,Y))))
␈↓ ↓H␈↓¬ ⊃∀X Y.(ISSEXP GOPHER CONS(X,Y)∧¬ATOM GOPHER CONS(X,Y)) ␈↓ε
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬ATOM X␈↓ε
␈↓ ↓H␈↓εMONADIC GOPHER_CONS,XISATM,CONSDF2,CONSDF3;
␈↓ ↓H␈↓ε␈↓¬9: ISSEXP GOPHER CONS(X,Y)∧¬ATOM GOPHER CONS(X,Y) (2)␈↓ε
␈↓ ↓H␈↓ε∀I ↑ Y; ⊃I XISATM⊃↑; ∀I ↑ X;
␈↓ ↓H␈↓ε␈↓¬GOODGO_ATM: 12: ∀X.(ATOM X⊃∀Y.(ISSEXP GOPHER CONS(X,Y)∧¬ATOM GOPHER CONS(X,Y)))␈↓ε
␈↓ ↓H␈↓␈↓ ¬pAppendix A␈↓ &xv
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬¬ATOM X␈↓ε
␈↓ ↓H␈↓ε␈↓αInduction hypothesis␈↓ε
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬GOODGOHYP: 13: ¬ATOM X∧(∀Y.(ISSEXP GOPHER CONS(CAR X,Y)∧¬ATOM GOPHER CONS(CAR X,Y))
␈↓ ↓H␈↓¬ ∧∀Y.(ISSEXP GOPHER CONS(CDR X,Y)∧¬ATOM GOPHER CONS(CDR X,Y))) (13)␈↓ε
␈↓ ↓H␈↓εMONADIC GOPHER_CONS,↑;
␈↓ ↓H␈↓ε␈↓¬14: ISSEXP GOPHER CONS(X,Y)∧¬ATOM GOPHER CONS(X,Y) (13)␈↓ε
␈↓ ↓H␈↓ε∀I ↑ Y; ⊃I GOODGOHYP⊃↑; ∀I ↑ X;
␈↓ ↓H␈↓ε␈↓¬GOODGO_NOTATM: 17: ∀X.((¬ATOM X
␈↓ ↓H␈↓¬ ∧(∀Y.(ISSEXP GOPHER CONS(CAR X,Y)∧¬ATOM GOPHER CONS(CAR X,Y))
␈↓ ↓H␈↓¬ ∧∀Y.(ISSEXP GOPHER CONS(CDR X,Y)∧¬ATOM GOPHER CONS(CDR X,Y))))
␈↓ ↓H␈↓¬ ⊃∀Y.(ISSEXP GOPHER CONS(X,Y)∧¬ATOM GOPHER CONS(X,Y))) ␈↓ε
␈↓ ↓H␈↓ε␈↓αApply induction axiom␈↓ε
␈↓ ↓H␈↓εTAUT GOODGO_IND,GOODGO_ATM,GOODGO_NOTATM;
␈↓ ↓H␈↓ε␈↓¬18: ∀X Y.(ISSEXP GOPHER CONS(X,Y)∧¬ATOM GOPHER CONS(X,Y)) ␈↓ε
␈↓ ↓H␈↓ε␈↓αNow use MAKEX to replace␈↓ε ␈↓¬CONS(X,Y)␈↓ε ␈↓αby␈↓ε ␈↓¬X␈↓ε
␈↓ ↓H␈↓ε∀E ↑ CAR X,CDR X; SUBSTR MAKEX IN ↑; MONADIC ↑,XISNTATM,CARDEF1,CDRDEF1;
␈↓ ↓H␈↓ε␈↓¬21: ISSEXP GOPHER X∧(ISSEXP CAR GOPHER X∧ISSEXP CDR GOPHER X) (3)␈↓ε
␈↓ ↓H␈↓ε⊃I XISNTATM⊃↑; ∀I ↑ X;
␈↓ ↓H␈↓ε␈↓¬GOOD_GOPHER: 23: ∀X.(¬ATOM X⊃(ISSEXP GOPHER X∧(ISSEXP CAR GOPHER X
␈↓ ↓H␈↓¬ ∧ISSEXP CDR GOPHER X)))␈↓ε
␈↓ ↓H␈↓α␈↓ ∧¬Proving ISTOT_FRINGE and GOOD_FRINGE
␈↓ ↓H␈↓ε␈↓αInduction axiom␈↓ε
␈↓ ↓H␈↓ε∧I SEXPINDUCTION [PHI←λX.(ISLIST FRINGE X∧¬NULL FRINGE X)];
␈↓ ↓H␈↓ε␈↓¬TOTFR_IND: 24: (∀X.(ATOM X⊃(ISLIST FRINGE X∧¬NULL FRINGE X))
␈↓ ↓H␈↓¬ ∧∀X.((¬ATOM X∧((ISLIST FRINGE CAR X∧¬NULL FRINGE CAR X)
␈↓ ↓H␈↓¬ ∧(ISLIST FRINGE CDR X∧¬NULL FRINGE CDR X)))
␈↓ ↓H␈↓¬ ⊃(ISLIST FRINGE X∧¬NULL FRINGE X)))
␈↓ ↓H␈↓¬ ⊃∀X.(ISLIST FRINGE X∧¬NULL FRINGE X) ␈↓ε
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬ATOM X␈↓ε
␈↓ ↓H␈↓εMONADIC FRINGEDEF,CONSDF1,CONSDF3,ATOMDF2;
␈↓ ↓H␈↓ε␈↓¬TOTFR_NUL: 25: ∀X.(ATOM X⊃(ISLIST FRINGE X∧¬NULL FRINGE X)) ␈↓ε
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬¬ATOM X␈↓ε
␈↓ ↓H␈↓ε∀E ISTOT_APPEND FRINGE CAR X,FRINGE CDR X;
␈↓ ↓H␈↓ε∀E NOTNUL_APPEND FRINGE CAR X,FRINGE CDR X;
␈↓ ↓H␈↓εMONADIC FRINGEDEF,↑↑:↑; ∀I ↑ X;
␈↓ ↓H␈↓ε␈↓¬29: ∀X.((¬ATOM X∧((ISLIST FRINGE CAR X∧¬NULL FRINGE CAR X)
␈↓ ↓H␈↓¬ ∧(ISLIST FRINGE CDR X∧¬NULL FRINGE CDR X)))
␈↓ ↓H␈↓¬ ⊃(ISLIST FRINGE X∧¬NULL FRINGE X)) ␈↓ε
␈↓ ↓H␈↓εTAUT TOTFR_IND,TOTFR_NUL,↑;
␈↓ ↓H␈↓ε␈↓¬ISTOT_FRINGE: 30: ∀X.(ISLIST FRINGE X∧¬NULL FRINGE X) ␈↓ε
␈↓ ↓H␈↓εMONADIC ↑,SORTAX,ATOMDF2;
␈↓ ↓H␈↓ε␈↓¬GOOD_FRINGE: 31: ∀X.(ISSEXP FRINGE X∧¬ATOM FRINGE X) ␈↓ε
␈↓ ↓H␈↓xvi␈↓ ¬pAppendix A␈↓ H
␈↓ ↓H␈↓α␈↓ ¬2Proving ISTOT_SIZE
␈↓ ↓H␈↓ε␈↓αInduction axiom␈↓ε
␈↓ ↓H␈↓ε∧I SEXPINDUCTION [PHI←λX.NATNUM(SIZE X)];
␈↓ ↓H␈↓ε␈↓¬TOTSIZE_IND: 32: (∀X.(ATOM X⊃NATNUM(SIZE X))
␈↓ ↓H␈↓¬ ∧∀X.((¬ATOM X∧(NATNUM(SIZE CAR X)∧NATNUM(SIZE CDR X)))⊃NATNUM(SIZE X)))
␈↓ ↓H␈↓¬ ⊃∀X.NATNUM(SIZE X) ␈↓ε
␈↓ ↓H␈↓εSIMPLIFY NATNUM(1); MONADIC ↑,SIZEDEF;
␈↓ ↓H␈↓ε␈↓¬TOTSIZE_ATM: 34: ∀X.(ATOM X⊃NATNUM(SIZE X)) ␈↓ε
␈↓ ↓H␈↓ε∀E ISTOT_PLUS SIZE CAR X,SIZE CDR X; MONADIC ↑,SIZEDEF; ∀I ↑ X;
␈↓ ↓H␈↓ε␈↓¬37: ∀X.((¬ATOM X∧(NATNUM(SIZE CAR X)∧NATNUM(SIZE CDR X)))⊃NATNUM(SIZE X)) ␈↓ε
␈↓ ↓H␈↓εTAUT TOTSIZE_IND,TOTSIZE_ATM,↑;
␈↓ ↓H␈↓ε␈↓¬ISTOT_SIZE: 38: ∀X.NATNUM(SIZE X) ␈↓ε
␈↓ ↓H␈↓α␈↓ ¬&Proving FRINGE_ATM
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬ATOM X ∧ ATOM Y␈↓ε
␈↓ ↓H␈↓εMONADIC FRINGEDEF;
␈↓ ↓H␈↓ε␈↓¬39: (ATOM X∧ATOM Y)⊃(FRINGE X=CONS(X,NNIL)∧FRINGE Y=CONS(Y,NNIL)) ␈↓ε
␈↓ ↓H␈↓ε∀E EQSEXP CONS(X,NNIL),CONS(Y,NNIL); MONADIC ↑↑:↑,CARDEF2,CDRDEF2,CONSDF3;
␈↓ ↓H␈↓ε␈↓¬FRATM: 41: (ATOM X∧ATOM Y)⊃(FRINGE X=FRINGE Y≡X=Y) ␈↓ε
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬ATOM X ∧ ¬ATOM Y␈↓ε
␈↓ ↓H␈↓ε␈↓αShow␈↓ε ␈↓¬¬NULL CDR (FRINGE CAR Y * FRINGE CDR Y)␈↓ε
␈↓ ↓H␈↓ε∀E CDR_APPEND FRINGE CAR Y,FRINGE CDR Y;
␈↓ ↓H␈↓ε∀E NOTNUL_APPEND CDR FRINGE CAR Y,FRINGE CDR Y;
␈↓ ↓H␈↓εMONADIC CARDEF1,CDRDEF1,ISTOT_FRINGE,CDRDEF3;
␈↓ ↓H␈↓ε␈↓¬44: ¬ATOM Y⊃(ISLIST FRINGE CAR Y∧(¬NULL FRINGE CAR Y
␈↓ ↓H␈↓¬ ∧(ISLIST FRINGE CDR Y∧(¬NULL FRINGE CDR Y∧ISLIST CDR FRINGE CAR Y)))) ␈↓ε
␈↓ ↓H␈↓εTAUTEQ ↑↑↑:↑;
␈↓ ↓H␈↓ε␈↓¬45: ¬ATOM Y⊃¬NULL CDR (FRINGE CAR Y*FRINGE CDR Y) ␈↓ε
␈↓ ↓H␈↓ε␈↓αNow combine this with EQSEXP to show␈↓ε ␈↓¬FRINGE X ≠ FRINGE Y␈↓ε
␈↓ ↓H␈↓ε∀E EQSEXP CONS(X,NNIL) , FRINGE CAR Y*FRINGE CDR Y;
␈↓ ↓H␈↓εMONADIC ATOMDF3,CDRDEF2,CONSDF3;
␈↓ ↓H␈↓ε␈↓¬47: NULL CDR CONS(X,NNIL) ␈↓ε
␈↓ ↓H␈↓εMONADIC FRINGEDEF;
␈↓ ↓H␈↓ε␈↓¬48: (ATOM X∧¬ATOM Y)⊃(FRINGE X=CONS(X,NNIL)∧FRINGE Y=(FRINGE CAR Y*FRINGE CDR Y)) ␈↓ε
␈↓ ↓H␈↓εMONADIC ↑↑↑↑:↑,GOOD_FRINGE,CONSDF3;
␈↓ ↓H␈↓ε␈↓¬49: (ATOM X∧¬ATOM Y)⊃(FRINGE X=FRINGE Y≡X=Y) ␈↓ε
␈↓ ↓H␈↓αExchange X and Y and combine the three cases.
␈↓ ↓H␈↓ε∀I ↑ X Y; ∀E ↑ Y X; TAUTEQ FRATM,↑↑↑:↑; ∀I ↑ X Y;
␈↓ ↓H␈↓ε␈↓¬FRINGE_ATM: 53: ∀X Y.((ATOM X∨ATOM Y)⊃(FRINGE X=FRINGE Y≡X=Y)) ␈↓ε
␈↓ ↓H␈↓␈↓ ¬pAppendix A␈↓ ∀xvii
␈↓ ↓H␈↓α␈↓ ¬λProving FRINGE_GOPHER
␈↓ ↓H␈↓ε␈↓αFirst prove the desired properties for argument␈↓ε ␈↓¬(CONS(X,Y))␈↓ε
␈↓ ↓H␈↓ε␈↓αInduction axiom␈↓ε
␈↓ ↓H␈↓ε∧I SEXPINDUCTION [PHI←λX.∀Y.(CAR FRINGE CONS(X,Y)=CAR GOPHER CONS(X,Y)
␈↓ ↓H␈↓ε ∧CDR FRINGE CONS(X,Y)=FRINGE CDR GOPHER CONS(X,Y))];
␈↓ ↓H␈↓ε␈↓¬FRGO_IND: 54: (∀X.(ATOM X⊃∀Y.(CAR FRINGE CONS(X,Y)=CAR GOPHER CONS(X,Y)
␈↓ ↓H␈↓¬ ∧CDR FRINGE CONS(X,Y)=FRINGE CDR GOPHER CONS(X,Y)))
␈↓ ↓H␈↓¬ ∧∀X.((¬ATOM X∧(∀Y.(CAR FRINGE CONS(CAR X,Y)=CAR GOPHER CONS(CAR X,Y)
␈↓ ↓H␈↓¬ ∧CDR FRINGE CONS(CAR X,Y)=FRINGE CDR GOPHER CONS(CAR X,Y))
␈↓ ↓H␈↓¬ ∧∀Y.(CAR FRINGE CONS(CDR X,Y)=CAR GOPHER CONS(CDR X,Y)
␈↓ ↓H␈↓¬ ∧CDR FRINGE CONS(CDR X,Y)=FRINGE CDR GOPHER CONS(CDR X,Y))))
␈↓ ↓H␈↓¬ ⊃∀Y.(CAR FRINGE CONS(X,Y)=CAR GOPHER CONS(X,Y)
␈↓ ↓H␈↓¬ ∧CDR FRINGE CONS(X,Y)=FRINGE CDR GOPHER CONS(X,Y))))
␈↓ ↓H␈↓¬ ⊃∀X Y.(CAR FRINGE CONS(X,Y)=CAR GOPHER CONS(X,Y)
␈↓ ↓H␈↓¬ ∧CDR FRINGE CONS(X,Y)=FRINGE CDR GOPHER CONS(X,Y)) ␈↓ε
␈↓ ↓H␈↓αSpecial case function definitions for FRINGE
␈↓ ↓H␈↓εMONADIC FRINGEDEF,CONSDF3;
␈↓ ↓H␈↓ε␈↓¬55: FRINGE CONS(X,Y)=(FRINGE CAR CONS(X,Y)*FRINGE CDR CONS(X,Y)) ␈↓ε
␈↓ ↓H␈↓εREWRITE FRINGE CONS(X,Y) BY {↑,CARCONS,CDRCONS};
␈↓ ↓H␈↓ε␈↓¬FRINGE_CONSXY: 56: FRINGE CONS(X,Y)=(FRINGE X*FRINGE Y) ␈↓ε
␈↓ ↓H␈↓ε∀I ↑ X Y;
␈↓ ↓H␈↓ε␈↓¬FRINGE_CONS: 57: ∀X Y.FRINGE CONS(X,Y)=(FRINGE X*FRINGE Y) ␈↓ε
␈↓ ↓H␈↓εMONADIC FRINGEDEF,XISATM;
␈↓ ↓H␈↓ε␈↓¬FRINGEATMX: 58: FRINGE X=CONS(X,NNIL) (2)␈↓ε
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬ATOM X␈↓ε
␈↓ ↓H␈↓ε␈↓αCAR part. Prove by a chain of eqialities.␈↓ε
␈↓ ↓H␈↓ε∀E CAR_APPEND FRINGE X,FRINGE Y; MONADIC ↑,ISTOT_FRINGE;
␈↓ ↓H␈↓ε␈↓¬60: CAR (FRINGE X*FRINGE Y)=CAR FRINGE X ␈↓ε
␈↓ ↓H␈↓εMONADIC CARDEF2;
␈↓ ↓H␈↓ε␈↓¬61: CAR CONS(X,NNIL)=CAR CONS(X,Y) ␈↓ε
␈↓ ↓H␈↓εSUBST FRINGE_CONSXY IN ↑↑; SUBSTR FRINGEATMX IN ↑;
␈↓ ↓H␈↓εSUBSTR ↑↑↑ IN ↑; SUBSTR GOPHER_CONS_ATM IN ↑ OCC 2;
␈↓ ↓H␈↓ε␈↓¬CARFRGO: 65: CAR FRINGE CONS(X,Y)=CAR GOPHER CONS(X,Y) (2)␈↓ε
␈↓ ↓H␈↓ε␈↓αCDR part. Prove by a chain of equalities.␈↓ε
␈↓ ↓H␈↓ε∀E CDR_APPEND FRINGE X, FRINGE Y; MONADIC ↑,ISTOT_FRINGE;
␈↓ ↓H␈↓ε␈↓¬67: CDR (FRINGE X*FRINGE Y)=(CDR FRINGE X*FRINGE Y) ␈↓ε
␈↓ ↓H␈↓ε∀E APPENDDEF NNIL FRINGE Y; MONADIC ↑,NULNIL,ISTOT_FRINGE;
␈↓ ↓H␈↓ε␈↓¬69: (NNIL*FRINGE Y)=FRINGE Y ␈↓ε
␈↓ ↓H␈↓εSUBST FRINGE_CONSXY IN ↑↑↑; SUBSTR FRINGEATMX IN ↑;
␈↓ ↓H␈↓ε␈↓¬71: CDR FRINGE CONS(X,Y)=(CDR CONS(X,NNIL)*FRINGE Y) (2)␈↓ε
␈↓ ↓H␈↓ε∀E CDRCONS X NNIL; SUBSTR ↑ IN ↑↑; SUBSTR ↑↑↑↑↑ IN ↑;
␈↓ ↓H␈↓ε␈↓¬74: CDR FRINGE CONS(X,Y)=FRINGE Y (2)␈↓ε
␈↓ ↓H␈↓ε∀E CDRDEF2 X Y; SUBSTR ↑ IN ↑↑ OCC 2;
␈↓ ↓H␈↓ε␈↓¬76: CDR FRINGE CONS(X,Y)=FRINGE CDR CONS(X,Y) (2)␈↓ε
␈↓ ↓H␈↓εSUBSTR GOPHER_CONS_ATM IN ↑ OCC 2;
␈↓ ↓H␈↓ε␈↓¬77: CDR FRINGE CONS(X,Y)=FRINGE CDR GOPHER CONS(X,Y) (2)␈↓ε
␈↓ ↓H␈↓ε␈↓αCombine CAR and CDR parts␈↓ε
␈↓ ↓H␈↓ε∧I CARFRGO ↑; ∀I ↑ Y; ⊃I XISATM⊃↑; ∀I ↑ X;
␈↓ ↓H␈↓ε␈↓¬FRGO_ATM: 81: ∀X.(ATOM X⊃∀Y.(CAR FRINGE CONS(X,Y)=CAR GOPHER CONS(X,Y)
␈↓ ↓H␈↓¬ ∧CDR FRINGE CONS(X,Y)=FRINGE CDR GOPHER CONS(X,Y))) ␈↓ε
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬¬ATOM X␈↓ε
␈↓ ↓H␈↓xviii␈↓ ¬pAppendix A␈↓ H
␈↓ ↓H␈↓ε␈↓αInduction hypothesis␈↓ε
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬FRGO_HYP: 82: ¬ATOM X∧(∀Y.(CAR FRINGE CONS(CAR X,Y)=CAR GOPHER CONS(CAR X,Y)
␈↓ ↓H␈↓¬ ∧CDR FRINGE CONS(CAR X,Y)=FRINGE CDR GOPHER CONS(CAR X,Y))
␈↓ ↓H␈↓¬ ∧∀Y.(CAR FRINGE CONS(CDR X,Y)=CAR GOPHER CONS(CDR X,Y)
␈↓ ↓H␈↓¬ ∧CDR FRINGE CONS(CDR X,Y)=FRINGE CDR GOPHER CONS(CDR X,Y))) (82)␈↓ε
␈↓ ↓H␈↓ε␈↓αProve␈↓ε ␈↓¬FRINGE CONS(X,Y)=FRINGE CONS(CAR X,CONS(CDR X,Y))␈↓ε ␈↓αby a chain of equalities␈↓ε
␈↓ ↓H␈↓εMONADIC FRINGEDEF,FRGO_HYP;
␈↓ ↓H␈↓ε␈↓¬FRGO1: 83: FRINGE X=(FRINGE CAR X*FRINGE CDR X) (82)␈↓ε
␈↓ ↓H␈↓ε∀E ASSOC_APPEND FRINGE CAR X,FRINGE CDR X,FRINGE Y;
␈↓ ↓H␈↓εMONADIC ↑,CARDEF1,CDRDEF1,ISTOT_FRINGE,FRGO_HYP;
␈↓ ↓H␈↓ε␈↓¬FRGO2: 85: ((FRINGE CAR X*FRINGE CDR X)*FRINGE Y)
␈↓ ↓H␈↓¬ =(FRINGE CAR X*(FRINGE CDR X*FRINGE Y)) (82)␈↓ε
␈↓ ↓H␈↓ε∀E FRINGE_CONS CDR X Y; MONADIC ↑,CDRDEF1,FRGO_HYP;
␈↓ ↓H␈↓ε␈↓¬FRGO3: 87: (FRINGE CDR X*FRINGE Y)=FRINGE CONS(CDR X,Y) (82)␈↓ε
␈↓ ↓H␈↓ε∀E FRINGE_CONS CAR X, CONS(CDR X,Y);
␈↓ ↓H␈↓εMONADIC ↑,CARDEF1,FRGO_HYP,GOOD_FRINGE;
␈↓ ↓H␈↓ε␈↓¬FRGO4: 89: (FRINGE CAR X*FRINGE CONS(CDR X,Y))
␈↓ ↓H␈↓¬ =FRINGE CONS(CAR X,CONS(CDR X,Y)) (82)␈↓ε
␈↓ ↓H␈↓εREWRITE FRINGE CONS(X,Y) BY {FRINGE_CONSXY,FRGO1,FRGO2,FRGO3,FRGO4};
␈↓ ↓H␈↓ε␈↓¬FR_ROTN: 90: FRINGE CONS(X,Y)=FRINGE CONS(CAR X,CONS(CDR X,Y)) (82)␈↓ε
␈↓ ↓H␈↓ε␈↓αInstantiate induction hypothesis␈↓ε
␈↓ ↓H␈↓εMONADIC FRGO_HYP;
␈↓ ↓H␈↓ε␈↓¬91: CAR FRINGE CONS(CAR X,CONS(CDR X,Y))=CAR GOPHER CONS(CAR X,CONS(CDR X,Y))
␈↓ ↓H␈↓¬ ∧CDR FRINGE CONS(CAR X,CONS(CDR X,Y))=FRINGE CDR GOPHER CONS(CAR X,CONS(CDR X,Y))
␈↓ ↓H␈↓¬ (82)␈↓ε
␈↓ ↓H␈↓ε␈↓αReplace␈↓ε ␈↓¬CONS(CAR X,CONS(CDR X,Y))␈↓ε ␈↓αby␈↓ε ␈↓¬CONS(X,Y)␈↓ε
␈↓ ↓H␈↓εTAUTEQ GOPHER_CONS,FRGO_HYP;
␈↓ ↓H␈↓ε␈↓¬92: GOPHER CONS(X,Y)=GOPHER CONS(CAR X,CONS(CDR X,Y)) (82)␈↓ε
␈↓ ↓H␈↓εSUBST ↑ IN ↑↑; SUBST FR_ROTN IN ↑;
␈↓ ↓H␈↓ε␈↓¬94: CAR FRINGE CONS(X,Y)=CAR GOPHER CONS(X,Y)
␈↓ ↓H␈↓¬ ∧CDR FRINGE CONS(X,Y)=FRINGE CDR GOPHER CONS(X,Y) (82)␈↓ε
␈↓ ↓H␈↓ε∀I ↑ Y; ⊃I FRGO_HYP⊃↑; ∀I ↑ X;
␈↓ ↓H␈↓ε␈↓¬97: ∀X.((¬ATOM X∧(∀Y.(CAR FRINGE CONS(CAR X,Y)=CAR GOPHER CONS(CAR X,Y)
␈↓ ↓H␈↓¬ ∧CDR FRINGE CONS(CAR X,Y)=FRINGE CDR GOPHER CONS(CAR X,Y))
␈↓ ↓H␈↓¬ ∧∀Y.(CAR FRINGE CONS(CDR X,Y)=CAR GOPHER CONS(CDR X,Y)
␈↓ ↓H␈↓¬ ∧CDR FRINGE CONS(CDR X,Y)=FRINGE CDR GOPHER CONS(CDR X,Y))))
␈↓ ↓H␈↓¬ ⊃∀Y.(CAR FRINGE CONS(X,Y)=CAR GOPHER CONS(X,Y)
␈↓ ↓H␈↓¬ ∧CDR FRINGE CONS(X,Y)=FRINGE CDR GOPHER CONS(X,Y))) ␈↓ε
␈↓ ↓H␈↓αApply induction axiom
␈↓ ↓H␈↓εTAUT FRGO_IND:#2 FRGO_IND,FRGO_ATM,↑;
␈↓ ↓H␈↓ε␈↓¬98: ∀X Y.(CAR FRINGE CONS(X,Y)=CAR GOPHER CONS(X,Y)
␈↓ ↓H␈↓¬ ∧CDR FRINGE CONS(X,Y)=FRINGE CDR GOPHER CONS(X,Y))␈↓ε
␈↓ ↓H␈↓ε␈↓αNow use MAKEX to replace␈↓ε ␈↓¬CONS(X,Y)␈↓ε ␈↓αby␈↓ε ␈↓¬X␈↓ε
␈↓ ↓H␈↓ε∀E ↑ CAR X,CDR X; SUBSTR MAKEX IN ↑;
␈↓ ↓H␈↓εMONADIC ↑,XISNTATM,CARDEF1,CDRDEF1;
␈↓ ↓H␈↓ε␈↓¬101: CAR FRINGE X=CAR GOPHER X∧CDR FRINGE X=FRINGE CDR GOPHER X (3)␈↓ε
␈↓ ↓H␈↓ε⊃I XISNTATM⊃↑; ∀I ↑ X;
␈↓ ↓H␈↓ε␈↓¬FRINGE_GOPHER: 103: ∀X.(¬ATOM X⊃(CAR FRINGE X=CAR GOPHER X
␈↓ ↓H␈↓¬ ∧CDR FRINGE X=FRINGE CDR GOPHER X)) ␈↓ε
␈↓ ↓H␈↓␈↓ ¬pAppendix A␈↓ ≥xix
␈↓ ↓H␈↓α␈↓ ¬(Proving EQU_FRINGE
␈↓ ↓H␈↓εTAUT ; SUBSTR XISY IN ↑ OCC 2; ⊃I XISY⊃↑;
␈↓ ↓H␈↓ε␈↓¬106: X=Y⊃FRINGE X=FRINGE Y ␈↓ε
␈↓ ↓H␈↓εTAUTEQ ↑; ∀I ↑ X Y;
␈↓ ↓H␈↓ε␈↓¬EQU_FRINGE: 108: ∀X Y.((X=Y∨FRINGE X=FRINGE Y)≡FRINGE X=FRINGE Y) ␈↓ε
␈↓ ↓H␈↓α␈↓ ¬!Proving SIZE_GOPHER
␈↓ ↓H␈↓ε␈↓αFirst show␈↓ε ␈↓¬SIZE CDR GOPHER CONS(X,Y)<SIZE CONS(X,Y)␈↓ε
␈↓ ↓H␈↓ε∀E SIZEDEF CONS(X,Y); MONADIC ↑,CONSDF3;
␈↓ ↓H␈↓ε␈↓¬110: SIZE CONS(X,Y)=(SIZE CAR CONS(X,Y)+SIZE CDR CONS(X,Y)) ␈↓ε
␈↓ ↓H␈↓εREWRITE SIZE CONS(X,Y) BY {↑,CARCONS,CDRCONS};
␈↓ ↓H␈↓ε␈↓¬SIZECONSXY: 111: SIZE CONS(X,Y)=(SIZE X+SIZE Y) ␈↓ε
␈↓ ↓H␈↓ε∀I ↑ X Y;
␈↓ ↓H␈↓ε␈↓¬SIZECONS: 112: ∀X Y.SIZE CONS(X,Y)=(SIZE X+SIZE Y) ␈↓ε
␈↓ ↓H␈↓ε␈↓αInduction axiom␈↓ε
␈↓ ↓H␈↓ε∧I SEXPINDUCTION [PHI←λX.∀Y.(SIZE CDR GOPHER CONS(X,Y)<SIZE CONS(X,Y))];
␈↓ ↓H␈↓ε␈↓¬LESS_IND: 113: (∀X.(ATOM X⊃∀Y.SIZE CDR GOPHER CONS(X,Y)<SIZE CONS(X,Y))
␈↓ ↓H␈↓¬ ∧∀X.((¬ATOM X∧(∀Y.SIZE CDR GOPHER CONS(CAR X,Y)<SIZE CONS(CAR X,Y)
␈↓ ↓H␈↓¬ ∧∀Y.SIZE CDR GOPHER CONS(CDR X,Y)<SIZE CONS(CDR X,Y)))
␈↓ ↓H␈↓¬ ⊃∀Y.SIZE CDR GOPHER CONS(X,Y)<SIZE CONS(X,Y)))
␈↓ ↓H␈↓¬ ⊃∀X Y.SIZE CDR GOPHER CONS(X,Y)<SIZE CONS(X,Y) ␈↓ε
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬ATOM X␈↓ε
␈↓ ↓H␈↓εTAUTEQ GOPHER CONS(X,Y)=CONS(X,Y) GOPHER_CONS,XISATM;
␈↓ ↓H␈↓εREWRITE SIZE CDR GOPHER CONS(X,Y) BY {↑,CDRCONS};
␈↓ ↓H␈↓ε␈↓¬115: SIZE CDR GOPHER CONS(X,Y)=SIZE Y (2)␈↓ε
␈↓ ↓H␈↓εMONADIC SIZE X=1 SIZEDEF XISATM; SUBSTR ↑ IN SIZECONSXY;
␈↓ ↓H␈↓ε␈↓¬117: SIZE CONS(X,Y)=(1+SIZE Y) (2)␈↓ε
␈↓ ↓H␈↓εMONADIC ↑↑↑:↑,ORDER,ISTOT_SIZE;
␈↓ ↓H␈↓ε␈↓¬118: SIZE CDR GOPHER CONS(X,Y)<SIZE CONS(X,Y) (2)␈↓ε
␈↓ ↓H␈↓ε∀I ↑ Y; ⊃I XISATM⊃↑; ∀I ↑ X;
␈↓ ↓H␈↓ε␈↓¬LESS_ATM: 121: ∀X.(ATOM X⊃∀Y.SIZE CDR GOPHER CONS(X,Y)<SIZE CONS(X,Y)) ␈↓ε
␈↓ ↓H␈↓ε␈↓αCase␈↓ε ␈↓¬¬ATOM X␈↓ε
␈↓ ↓H␈↓ε␈↓αInduction hypothesis␈↓ε
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬LESS_HYP: 122: ¬ATOM X∧(∀Y.SIZE CDR GOPHER CONS(CAR X,Y)<SIZE CONS(CAR X,Y)
␈↓ ↓H␈↓¬ ∧∀Y.SIZE CDR GOPHER CONS(CDR X,Y)<SIZE CONS(CDR X,Y)) (122)␈↓ε
␈↓ ↓H␈↓ε␈↓αShow␈↓ε ␈↓¬SIZE CONS(CAR X,CONS(CDR X,Y))=SIZE CONS(X,Y)␈↓ε ␈↓αusing a chain of equalities␈↓ε
␈↓ ↓H␈↓ε∀E SIZECONS CAR X,CONS(CDR X,Y); MONADIC ↑,CARDEF1,LESS_HYP;
␈↓ ↓H␈↓ε␈↓¬LESS1: 124: SIZE CONS(CAR X,CONS(CDR X,Y))=(SIZE CAR X+SIZE CONS(CDR X,Y)) (122)␈↓ε
␈↓ ↓H␈↓ε∀E SIZECONS CDR X,Y; MONADIC ↑,CDRDEF1,LESS_HYP;
␈↓ ↓H␈↓ε␈↓¬LESS2: 126: SIZE CONS(CDR X,Y)=(SIZE CDR X+SIZE Y) (122)␈↓ε
␈↓ ↓H␈↓ε∀E ASSOC SIZE CAR X,SIZE CDR X,SIZE Y;
␈↓ ↓H␈↓εMONADIC ↑,ISTOT_SIZE,CARDEF1,CDRDEF1,LESS_HYP;
␈↓ ↓H␈↓ε␈↓¬LESS3: 128: (SIZE CAR X+(SIZE CDR X+SIZE Y))=((SIZE CAR X+SIZE CDR X)+SIZE Y) (122)␈↓ε
␈↓ ↓H␈↓εMONADIC SIZEDEF,LESS_HYP;
␈↓ ↓H␈↓ε␈↓¬LESS4: 129: (SIZE CAR X+SIZE CDR X)=SIZE X (122)␈↓ε
␈↓ ↓H␈↓εTAUTEQ SIZECONSXY;
␈↓ ↓H␈↓ε␈↓¬LESS5: 130: (SIZE X+SIZE Y)=SIZE CONS(X,Y) ␈↓ε
␈↓ ↓H␈↓εREWRITE SIZE CONS(CAR X,CONS(CDR X,Y)) BY {LESS1,LESS2,LESS3,LESS4,LESS5};
␈↓ ↓H␈↓ε␈↓¬131: SIZE CONS(CAR X,CONS(CDR X,Y))=SIZE CONS(X,Y) (122)␈↓ε
␈↓ ↓H␈↓xx␈↓ ¬pAppendix A␈↓ H
␈↓ ↓H␈↓ε␈↓αShow␈↓ε ␈↓¬SIZE CDR GOPHER CONS(X,Y)=SIZE CDR GOPHER CONS(CAR X,CONS(CDR X,Y))␈↓ε
␈↓ ↓H␈↓εTAUTEQ GOPHER CONS(X,Y)=GOPHER CONS(CAR X,CONS(CDR X,Y))
␈↓ ↓H␈↓ε GOPHER_CONS,LESS_HYP;
␈↓ ↓H␈↓εREWRITE SIZE CDR GOPHER CONS(X,Y) BY {↑};
␈↓ ↓H␈↓ε␈↓¬133: SIZE CDR GOPHER CONS(X,Y)=SIZE CDR GOPHER CONS(CAR X,CONS(CDR X,Y)) (122)␈↓ε
␈↓ ↓H␈↓αCombine these two equalities with the induction hypothesis
␈↓ ↓H␈↓εMONADIC SIZE CDR GOPHER CONS(X,Y)<SIZE CONS(X,Y) ↑↑↑,↑,LESS_HYP;
␈↓ ↓H␈↓ε∀I ↑ Y; ⊃I LESS_HYP⊃↑; ∀I ↑ X;
␈↓ ↓H␈↓ε␈↓¬137: ∀X.((¬ATOM X∧(∀Y.SIZE CDR GOPHER CONS(CAR X,Y)<SIZE CONS(CAR X,Y)
␈↓ ↓H␈↓¬ ∧∀Y.SIZE CDR GOPHER CONS(CDR X,Y)<SIZE CONS(CDR X,Y)))
␈↓ ↓H␈↓¬ ⊃∀Y.SIZE CDR GOPHER CONS(X,Y)<SIZE CONS(X,Y)) ␈↓ε
␈↓ ↓H␈↓αApply induction axiom
␈↓ ↓H␈↓εTAUT LESS_IND,LESS_ATM,↑;
␈↓ ↓H␈↓ε␈↓¬138: ∀X Y.SIZE CDR GOPHER CONS(X,Y)<SIZE CONS(X,Y) ␈↓ε
␈↓ ↓H␈↓ε␈↓αNow show␈↓ε ␈↓¬¬ATOM X⊃SIZE CDR GOPHER X<SIZE X␈↓ε
␈↓ ↓H␈↓ε∀E ↑ CAR X,CDR X; SUBSTR MAKEX IN ↑;
␈↓ ↓H␈↓εMONADIC ↑,CARDEF1,CDRDEF1,XISNTATM; ⊃I XISNTATM⊃↑; ∀I ↑ X;
␈↓ ↓H␈↓ε␈↓¬LESS_CDR_GOPHER: 143: ∀X.(¬ATOM X⊃SIZE CDR GOPHER X<SIZE X) ␈↓ε
␈↓ ↓H␈↓αApply the axiom relation < and + to the above relation to obtain SIZE_GOPHER
␈↓ ↓H␈↓ε∀E ORDER_PLUS SIZE CDR GOPHER X,SIZE X,SIZE CDR GOPHER Y,SIZE Y;
␈↓ ↓H␈↓εMONADIC ↑,LESS_CDR_GOPHER,ISTOT_SIZE,GOOD_GOPHER; ∀I ↑ X Y;
␈↓ ↓H␈↓ε␈↓¬SIZE_GOPHER: 146: ∀X Y.((¬ATOM X∧¬ATOM Y)
␈↓ ↓H␈↓¬ ⊃(SIZE CDR GOPHER X+SIZE CDR GOPHER Y)<(SIZE X+SIZE Y))␈↓ε
␈↓ ↓H␈↓␈↓ ¬pAppendix A␈↓ ≥xxi
␈↓ ↓H␈↓α␈↓ ∧SProving the SAMEFRINGE theorem
␈↓ ↓H␈↓αA brief summary of the proof
␈↓ ↓H␈↓ ␈↓↓The theorem, ␈↓¬THM, ␈↓↓is the conjunct of three theorems:
␈↓ ↓H␈↓↓ ␈↓¬THM1: ISTV SAMEFRINGEA(X,Y),␈↓↓
␈↓ ↓H␈↓↓ ␈↓¬THM2: SAMEFRINGE(X,Y)=(X=Y)∨((¬ATOM X∧¬ATOM Y)∧SAME(GOPHER X,GOPHER Y)),␈↓↓
␈↓ ↓H␈↓↓ ␈↓¬THM3: SAMEFRINGE(X,Y)≡FRINGE X=FRINGE Y.␈↓↓
␈↓ ↓H␈↓↓The proof is by induction on ␈↓¬SIZE X+SIZE Y.␈↓↓ The induction predicate is
␈↓ ↓H␈↓↓ ␈↓¬PSI(N)≡∀X Y.(SIZE X+SIZE Y=N⊃THM(X,Y)).␈↓↓
␈↓ ↓H␈↓↓To␈α⊗prove␈α⊗that␈α⊗the␈α⊗theorem␈α⊗follows␈α⊗from␈α⊗the␈α⊗induction␈α⊗hypothesis␈α⊗two␈α⊗cases␈α⊗are␈α⊗considered:
␈↓ ↓H␈↓↓␈↓¬ATOMXY≡(ATOM X ∨ ATOM Y)␈↓↓␈α∂and␈α∂␈↓¬NOTATMXY≡(¬ATOM X ∧¬ATOM Y).␈↓↓␈α∂ In␈α∂the␈α∂first␈α∂case␈α∂the␈α∞theorem
␈↓ ↓H␈↓↓follows␈α
by␈αdirect␈α
computation.␈α
In␈αthe␈α
second␈α
case␈α ␈↓¬THMCDRGO≡THM(CDR GOPHER X, CDR GOPHER Y)␈↓↓
␈↓ ↓H␈↓↓is derived from the induction hypothesis and used to prove the theorem for ␈↓¬(X,Y).␈↓↓
␈↓ ↓H␈↓αLemmas needed for proof of SAMEFRINGE theorem
␈↓ ↓H␈↓¬AXIOM TV_LEMMAS:
␈↓ ↓H␈↓¬ TVEEQ: ∀X Y.(ISTV X EEQ Y) ;
␈↓ ↓H␈↓¬ EQEEQ: ∀X Y.((X EEQ Y=TT)≡(X=Y));
␈↓ ↓H␈↓¬ TVOOR: ∀P Q.(ISTV(P OOR Q));
␈↓ ↓H␈↓¬ TVAAND: ∀P Q.(ISTV(P AAND Q));
␈↓ ↓H␈↓¬ EQOOR: ∀P Q.((P OOR Q = TT)≡(P=TT ∨ Q=TT));
␈↓ ↓H␈↓¬ EQAAND: ∀P Q.((P AAND Q = TT)≡(P=TT ∧ Q=TT));
␈↓ ↓H␈↓¬ TVNOTATM: ∀X Y.((ISTV (NNOT AATOM X AAND NNOT AATOM Y)));
␈↓ ↓H␈↓¬ EQNOTATM: ∀X Y.((((NNOT AATOM X AAND NNOT AATOM Y)= TT)≡(¬ATOM X∧¬ATOM Y)));
␈↓ ↓H␈↓¬ POORF: ∀P.(P OOR FF=P);
␈↓ ↓H␈↓¬ FAANDQQ: ∀QQ.(FF AAND QQ = FF);
␈↓ ↓H␈↓¬;;
␈↓ ↓H␈↓¬AXIOM GOPHER_FRINGE_SIZE_LEMMAS:
␈↓ ↓H␈↓¬ GOOD_GOPHER: ∀X.((¬ATOM X)⊃(ISSEXP GOPHER X∧ISSEXP CAR GOPHER X
␈↓ ↓H␈↓¬ ∧ISSEXP CDR GOPHER X));
␈↓ ↓H␈↓¬ GOOD_FRINGE: ∀X.(¬ATOM FRINGE X∧ISSEXP FRINGE X);
␈↓ ↓H␈↓¬ ISTOT_SIZE: ∀X.NATNUM(SIZE X);
␈↓ ↓H␈↓¬ FRINGE_ATM: ∀X Y.((ATOM X∨ATOM Y)⊃((FRINGE X=FRINGE Y)≡(X=Y)));
␈↓ ↓H␈↓¬ FRINGE_GOPHER:
␈↓ ↓H␈↓¬ ∀X.((¬ATOM X)⊃(CAR FRINGE X=CAR GOPHER X∧CDR FRINGE X=FRINGE CDR GOPHER X));
␈↓ ↓H␈↓¬ EQU_FRINGE: ∀X Y.(X=Y∨FRINGE X=FRINGE Y≡FRINGE X=FRINGE Y);
␈↓ ↓H␈↓¬ SIZE_GOPHER:
␈↓ ↓H␈↓¬ ∀X Y.((¬ATOM X∧¬ATOM Y)
␈↓ ↓H␈↓¬ ⊃((SIZE CDR GOPHER X+SIZE CDR GOPHER Y)<(SIZE X+SIZE Y)));
␈↓ ↓H␈↓¬;;
␈↓ ↓H␈↓xxii␈↓ ¬pAppendix A␈↓ H
␈↓ ↓H␈↓αStandard hypotheses
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬XISATM: 1: ATOM X (1)␈↓ε
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬YISATM: 2: ATOM Y (2)␈↓ε
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬ATOMXY: 3: ATOM X∨ATOM Y (3)␈↓ε
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬NOTATMXY: 4: ¬ATOM X∧¬ATOM Y (4)␈↓ε
␈↓ ↓H␈↓εTAUT ;
␈↓ ↓H␈↓ε␈↓¬ATOMXYTAUT: 5: (ATOM X∨ATOM Y)∨(¬ATOM X∧¬ATOM Y) ␈↓ε
␈↓ ↓H␈↓αDefinitions
␈↓ ↓H␈↓ε∀E SAMEFRINGEADEF X,Y;
␈↓ ↓H␈↓ε␈↓¬SFAXY: 6: SAMEFRINGEA(X,Y)=((X EEQ Y) OOR ((NNOT AATOM X AAND NNOT AATOM Y)
␈↓ ↓H␈↓¬ AAND SAMEA(GOPHER X,GOPHER Y))) ␈↓ε
␈↓ ↓H␈↓ε∀E SAMEFRINGEDEF X,Y;
␈↓ ↓H␈↓ε␈↓¬SFXY: 7: SAMEFRINGE(X,Y)≡SAMEFRINGEA(X,Y)=TT ␈↓ε
␈↓ ↓H␈↓ε∀E THMDEF X Y;
␈↓ ↓H␈↓ε␈↓¬THMXY: 8: THM(X,Y)≡(ISTV SAMEFRINGEA(X,Y)
␈↓ ↓H␈↓¬ ∧((SAMEFRINGE(X,Y)≡(X=Y∨((¬ATOM X∧¬ATOM Y)∧SAME(GOPHER X,GOPHER Y))))
␈↓ ↓H␈↓¬ ∧(SAMEFRINGE(X,Y)≡FRINGE X=FRINGE Y))) ␈↓ε
␈↓ ↓H␈↓αUseful facts in useful form
␈↓ ↓H␈↓ε∀E TVNOTATM X Y;
␈↓ ↓H␈↓ε␈↓¬TVNOTATMXY: 9: ISTV (NNOT AATOM X AAND NNOT AATOM Y) ␈↓ε
␈↓ ↓H␈↓ε∀E EQNOTATM X Y;
␈↓ ↓H␈↓ε␈↓¬EQNOTATMXY: 10: (NNOT AATOM X AAND NNOT AATOM Y)=TT≡(¬ATOM X∧¬ATOM Y) ␈↓ε
␈↓ ↓H␈↓ε∀E TVEEQ X,Y;
␈↓ ↓H␈↓ε␈↓¬TVEEQXY: 11: ISTV (X EEQ Y) ␈↓ε
␈↓ ↓H␈↓ε∀E EQEEQ X,Y;
␈↓ ↓H␈↓ε␈↓¬EQEEQXY: 12: (X EEQ Y)=TT≡X=Y ␈↓ε
␈↓ ↓H␈↓ε∀E GOOD_GOPHER X;
␈↓ ↓H␈↓ε␈↓¬GOODGOX: 13: ¬ATOM X⊃(ISSEXP GOPHER X∧(ISSEXP CAR GOPHER X∧ISSEXP CDR GOPHER X))␈↓ε
␈↓ ↓H␈↓ε∀E GOOD_GOPHER Y;
␈↓ ↓H␈↓ε␈↓¬GOODGOY: 14: ¬ATOM Y⊃(ISSEXP GOPHER Y∧(ISSEXP CAR GOPHER Y∧ISSEXP CDR GOPHER Y))␈↓ε
␈↓ ↓H␈↓α␈↓ ¬←Case ATOMXY
␈↓ ↓H␈↓ε␈↓αCompute␈↓ε ␈↓¬SAMEFRINGEA(X,Y)␈↓ε
␈↓ ↓H␈↓ε∀E FAANDQQ SAMEA(GOPHER X, GOPHER Y); ∀E ETVSAMEA GOPHER X,GOPHER Y;
␈↓ ↓H␈↓ε⊃E ↑,↑↑;
␈↓ ↓H␈↓ε␈↓¬17: (FF AAND SAMEA(GOPHER X,GOPHER Y))=FF ␈↓ε
␈↓ ↓H␈↓ε∀E TVEXT NNOT AATOM X AAND NNOT AATOM Y;
␈↓ ↓H␈↓εTAUT TVSET,TVNOTATMXY,EQNOTATMXY,ATOMXY,↑;
␈↓ ↓H␈↓ε␈↓¬19: (NNOT AATOM X AAND NNOT AATOM Y)=FF (3)␈↓ε
␈↓ ↓H␈↓ε∀E POORF X EEQ Y;
␈↓ ↓H␈↓εTAUT ↑,TVEEQXY;
␈↓ ↓H␈↓ε␈↓¬21: ((X EEQ Y) OOR FF)=(X EEQ Y) ␈↓ε
␈↓ ↓H␈↓εREWRITE SAMEFRINGEA(X,Y) BY {SFAXY,↑↑↑,↑↑↑↑↑,↑};
␈↓ ↓H␈↓ε␈↓¬22: SAMEFRINGEA(X,Y)=(X EEQ Y) (3)␈↓ε
␈↓ ↓H␈↓␈↓ ¬pAppendix A␈↓ xxiii
␈↓ ↓H␈↓αUse properties of EEQ and FRINGE of an atom to conclude THM
␈↓ ↓H␈↓ε∀E FRINGE_ATM X Y;
␈↓ ↓H␈↓εTAUTEQ THMXY,SFXY,ATOMXY,TVEEQXY,EQEEQXY,↑↑:↑;
␈↓ ↓H␈↓ε␈↓¬THMATM: 24: THM(X,Y) (3)␈↓ε
␈↓ ↓H␈↓α␈↓ ¬HCase NOTATMXY
␈↓ ↓H␈↓ε␈↓αInduction axiom␈↓ε
␈↓ ↓H␈↓ε∧I NUMBINDUCTION [PSI←λN.∀X Y.((SIZE X+SIZE Y=N)⊃THM(X,Y))];
␈↓ ↓H␈↓ε␈↓¬SFIND: 25: ∀N.(∀M.(M<N⊃∀X Y.((SIZE X+SIZE Y)=M⊃THM(X,Y)))
␈↓ ↓H␈↓¬ ⊃∀X Y.((SIZE X+SIZE Y)=N⊃THM(X,Y)))⊃∀N X Y.((SIZE X+SIZE Y)=N⊃THM(X,Y)) ␈↓ε
␈↓ ↓H␈↓ε␈↓αInduction hypotheses␈↓ε
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬NHYP: 26: (SIZE X+SIZE Y)=N (26)␈↓ε
␈↓ ↓H␈↓εASSUME ;
␈↓ ↓H␈↓ε␈↓¬SFINDHYP: 27: ∀M.(M<N⊃∀X Y.((SIZE X+SIZE Y)=M⊃THM(X,Y))) (27)␈↓ε
␈↓ ↓H␈↓αShow THMCDRGO
␈↓ ↓H␈↓ε∀E SFINDHYP SIZE CDR GOPHER X+SIZE CDR GOPHER Y;
␈↓ ↓H␈↓εMONADIC ISTOT_PLUS,ISTOT_SIZE,GOOD_GOPHER,NOTATMXY;
␈↓ ↓H␈↓ε␈↓¬29: NATNUM(SIZE CDR GOPHER X+SIZE CDR GOPHER Y) (4)␈↓ε
␈↓ ↓H␈↓ε∀E SIZE_GOPHER X Y; TAUTEQ ↑↑↑:↑,NOTATMXY,NHYP;
␈↓ ↓H␈↓ε␈↓¬31: ∀X1 Y1.((SIZE X1+SIZE Y1)=(SIZE CDR GOPHER X+SIZE CDR GOPHER Y)⊃THM(X1,Y1))
␈↓ ↓H␈↓¬ (4 26 27)␈↓ε
␈↓ ↓H␈↓ε∀E ↑ CDR GOPHER X,CDR GOPHER Y; TAUTEQ ↑, NOTATMXY, GOODGOX,GOODGOY;
␈↓ ↓H␈↓ε␈↓¬THMCDRGO: 33: THM(CDR GOPHER X,CDR GOPHER Y) (4 26 27)␈↓ε
␈↓ ↓H␈↓αInstantiate the lemmas and definitions to be used.
␈↓ ↓H␈↓ε␈↓¬CASENOTATM: ␈↓ε
␈↓ ↓H␈↓αProving THM1
␈↓ ↓H␈↓ε∀E TVEEQ CAR GOPHER X,CAR GOPHER Y;
␈↓ ↓H␈↓ε∀E TVAAND CAR GOPHER X EEQ CAR GOPHER Y,SAMEFRINGEA(CDR GOPHER X,CDR GOPHER Y);
␈↓ ↓H␈↓ε∀E TVAAND NNOT AATOM X AAND NNOT AATOM Y,SAMEA(GOPHER X,GOPHER Y);
␈↓ ↓H␈↓ε∀E TVOOR X EEQ Y,(NNOT AATOM X AAND NNOT AATOM Y) AAND SAMEA(GOPHER X,GOPHER Y);
␈↓ ↓H␈↓αProving THM2
␈↓ ↓H␈↓ε∀E EQOOR X EEQ Y,(NNOT AATOM X AAND NNOT AATOM Y) AAND SAMEA(GOPHER X,GOPHER Y);
␈↓ ↓H␈↓ε∀E EQAAND NNOT AATOM X AAND NNOT AATOM Y,SAMEA(GOPHER X,GOPHER Y);
␈↓ ↓H␈↓ε∀E SAMEDEF GOPHER X,GOPHER Y;
␈↓ ↓H␈↓xxiv␈↓ ¬pAppendix A␈↓ H
␈↓ ↓H␈↓αProving THM3
␈↓ ↓H␈↓ε␈↓αShow␈↓ε ␈↓¬SAMEA(GOPHER X,GOPHER Y)=TT≡
␈↓ ↓H␈↓¬ (CAR GOPHER X=CAR GOPHER Y)∧(FRINGE CDR GOPHER X=FRINGE CDR GOPHER Y)␈↓ε
␈↓ ↓H␈↓ε∀E SAMEADEF GOPHER X,GOPHER Y;
␈↓ ↓H␈↓ε∀E SAMEFRINGEDEF CDR GOPHER X, CDR GOPHER Y;
␈↓ ↓H␈↓ε∀E THMDEF CDR GOPHER X,CDR GOPHER Y;
␈↓ ↓H␈↓ε∀E EQAAND CAR GOPHER X EEQ CAR GOPHER Y,SAMEFRINGEA(CDR GOPHER X,CDR GOPHER Y);
␈↓ ↓H␈↓ε∀E EQEEQ CAR GOPHER X,CAR GOPHER Y;
␈↓ ↓H␈↓ε␈↓αThen use FRINGE_GOPHER to get␈↓ε
␈↓ ↓H␈↓ε ␈↓¬SAMEFRINGE(X,Y)≡X=Y∨(CAR FRINGE X=CAR FRINGE Y∧CDR FRINGE X=CDR FRINGE Y)␈↓ε
␈↓ ↓H␈↓ε∀E FRINGE_GOPHER X;
␈↓ ↓H␈↓ε∀E FRINGE_GOPHER Y;
␈↓ ↓H␈↓ε␈↓αNow use EQSEXP to replace the second disjunct by␈↓ε ␈↓¬FRINGE X=FRINGE Y␈↓ε
␈↓ ↓H␈↓ε ␈↓αand EQU_FRINGE to absorb the␈↓ε ␈↓¬X=Y␈↓ε
␈↓ ↓H␈↓ε∀E GOOD_FRINGE X;
␈↓ ↓H␈↓ε∀E GOOD_FRINGE Y;
␈↓ ↓H␈↓ε∀E EQSEXP FRINGE X,FRINGE Y;
␈↓ ↓H␈↓ε∀E EQU_FRINGE X Y;
␈↓ ↓H␈↓ε␈↓αConclude THM in case NOTATMXY␈↓ε
␈↓ ↓H␈↓εTAUTEQ CASENOTATM:↑,GOODGOX,GOODGOY,NOTATMXY,THMCDRGO,
␈↓ ↓H␈↓ε SFAXY,TVNOTATMXY,TVEEQXY,
␈↓ ↓H␈↓ε SFXY,EQNOTATMXY,EQEEQXY,THMXY;
␈↓ ↓H␈↓ε␈↓¬52: THM(X,Y) (4 26 27)␈↓ε
␈↓ ↓H␈↓αCombine the cases ATMXY and NOTATMXY
␈↓ ↓H␈↓ε∨E ATOMXYTAUT THMATM,↑;
␈↓ ↓H␈↓ε␈↓¬53: THM(X,Y) (26 27)␈↓ε
␈↓ ↓H␈↓αComplete the induction
␈↓ ↓H␈↓ε⊃I NHYP⊃↑; ∀I ↑ X Y;
␈↓ ↓H␈↓ε⊃I SFINDHYP⊃↑; ∀I ↑ N;
␈↓ ↓H␈↓ε␈↓¬57: ∀N.(∀M.(M<N⊃∀X Y.((SIZE X+SIZE Y)=M⊃THM(X,Y)))⊃∀X Y.((SIZE X+SIZE Y)=N⊃THM(X,Y)))␈↓ε
␈↓ ↓H␈↓ε⊃E ↑,SFIND;
␈↓ ↓H␈↓ε␈↓¬58: ∀N X Y.((SIZE X+SIZE Y)=N⊃THM(X,Y)) ␈↓ε
␈↓ ↓H␈↓αExtract THM from induction predicate
␈↓ ↓H␈↓εMONADIC ISTOT_PLUS,ISTOT_SIZE;
␈↓ ↓H␈↓ε␈↓¬59: NATNUM(SIZE X+SIZE Y) ␈↓ε
␈↓ ↓H␈↓ε∀E ↑↑ SIZE X + SIZE Y; ⊃E ↑,↑↑;
␈↓ ↓H␈↓ε␈↓¬61: ∀X1 Y1.((SIZE X1+SIZE Y1)=(SIZE X+SIZE Y)⊃THM(X1,Y1)) ␈↓ε
␈↓ ↓H␈↓ε∀E ↑ X Y ; TAUT ↑; ∀I ↑ X Y;
␈↓ ↓H␈↓ε␈↓¬SFTHM: 64: ∀X Y.THM(X,Y) ␈↓ε
␈↓ ↓H␈↓␈↓ εH␈↓ ∃xxv
␈↓ ↓H␈↓α␈↓ ¬vAppendix B
␈↓ ↓H␈↓α␈↓ ¬\LISP Compilers
␈↓ ↓H␈↓1. ␈↓αMACLISP listing of LCOM0␈↓
␈↓ ↓H␈↓ε(DECLARE (SETQ NO-DISK-HACKS T))
␈↓ ↓H␈↓ε(DECLARE (READ))
␈↓ ↓H␈↓ε(DEFPROP LC0FNS
␈↓ ↓H␈↓ε (LC0FNS COMPL COMP PRUP MKPUSH COMPEXP COMPLIS LOADAC COMCOND COMBOOL COMPANDOR)
␈↓ ↓H␈↓εVALUE)
␈↓ ↓H␈↓ε(DEFPROP COMPL
␈↓ ↓H␈↓ε (LAMBDA(FILE)
␈↓ ↓H␈↓ε (UWRITE)
␈↓ ↓H␈↓ε (APPLY (QUOTE EREAD) FILE)
␈↓ ↓H␈↓ε (SELECT-DISK-INPUT
␈↓ ↓H␈↓ε (READ-UNTIL-EOF
␈↓ ↓H␈↓ε WITH
␈↓ ↓H␈↓ε Z
␈↓ ↓H␈↓ε DO
␈↓ ↓H␈↓ε (COND ((OR (EQ (CAR Z) (QUOTE DEFUN)) (AND (EQ (CAR Z) (QUOTE DEFPROP)) (EQ (CADDDR Z) (QUOTE EXPR))))
␈↓ ↓H␈↓ε (PROG (PROG)
␈↓ ↓H␈↓ε (SETQ PROG
␈↓ ↓H␈↓ε (COND ((EQ (CAR Z) (QUOTE DEFUN)) (COMP (CADR Z) (CADDR Z) (CADDDR Z)))
␈↓ ↓H␈↓ε (T (COMP (CADR Z) (CADR (CADDR Z)) (CADDR (CADDR Z))))))
␈↓ ↓H␈↓ε (UNSELECT-TTY (SELECT-DISK-OUTPUT (MAPC (FUNCTION PRINT) PROG)))
␈↓ ↓H␈↓ε (PRINT (LIST (CADR Z) (LENGTH PROG)))))
␈↓ ↓H␈↓ε (T (UNSELECT-TTY (SELECT-DISK-OUTPUT (PRINT Z))))))
␈↓ ↓H␈↓ε (APPLY (QUOTE UFILE) (LIST (CAR FILE) (QUOTE LAP)))
␈↓ ↓H␈↓ε (QUOTE ENDCOMP)))
␈↓ ↓H␈↓εFEXPR)
␈↓ ↓H␈↓ε(DEFPROP COMP
␈↓ ↓H␈↓ε (LAMBDA(FN VARS EXP)
␈↓ ↓H␈↓ε ((LAMBDA(N)
␈↓ ↓H␈↓ε (APPEND (LIST (LIST (QUOTE LAP) FN (QUOTE SUBR)))
␈↓ ↓H␈↓ε (MKPUSH N 1)
␈↓ ↓H␈↓ε (COMPEXP EXP (MINUS N) (PRUP VARS 1))
␈↓ ↓H␈↓ε (LIST (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE %) 0 0 N N)))
␈↓ ↓H␈↓ε (QUOTE ((POPJ P) NIL))))
␈↓ ↓H␈↓ε (LENGTH VARS)))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP PRUP
␈↓ ↓H␈↓ε (LAMBDA (VARS N) (COND ((NULL VARS) NIL) (T (CONS (CONS (CAR VARS) N) (PRUP (CDR VARS) (ADD1 N))))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP MKPUSH
␈↓ ↓H␈↓ε (LAMBDA (N M) (COND ((LESSP N M) NIL) (T (CONS (LIST (QUOTE PUSH) (QUOTE P) M) (MKPUSH N (ADD1 M))))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP COMPEXP
␈↓ ↓H␈↓ε (LAMBDA(EXP M VPR)
␈↓ ↓H␈↓ε (COND ((NULL EXP) (QUOTE ((MOVEI 1 0))))
␈↓ ↓H␈↓ε ((EQ EXP T) (QUOTE ((MOVEI 1 (QUOTE T)))))
␈↓ ↓H␈↓ε ((NUMBERP EXP) (LIST (LIST (QUOTE MOVEI) 1 (LIST (QUOTE QUOTE) EXP))))
␈↓ ↓H␈↓ε ((ATOM EXP) (LIST (LIST (QUOTE MOVE) 1 (PLUS M (CDR (ASSOC EXP VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε ((OR (EQ (CAR EXP) (QUOTE AND)) (EQ (CAR EXP) (QUOTE OR)) (EQ (CAR EXP) (QUOTE NOT)))
␈↓ ↓H␈↓ε ((LAMBDA(L1 L2)
␈↓ ↓H␈↓ε (APPEND (COMBOOL EXP M L1 NIL VPR)
␈↓ ↓H␈↓xxvi␈↓ ¬qAppendix B␈↓ H
␈↓ ↓H␈↓ε (LIST (QUOTE (MOVEI 1 (QUOTE T))) (LIST (QUOTE JRST) 0 L2) L1 (QUOTE (MOVEI 1 0)) L2)))
␈↓ ↓H␈↓ε (GENSYM)
␈↓ ↓H␈↓ε (GENSYM)))
␈↓ ↓H␈↓ε ((EQ (CAR EXP) (QUOTE COND)) (COMCOND (CDR EXP) M (GENSYM) VPR))
␈↓ ↓H␈↓ε ((EQ (CAR EXP) (QUOTE QUOTE)) (LIST (LIST (QUOTE MOVEI) 1 EXP)))
␈↓ ↓H␈↓ε ((ATOM (CAR EXP))
␈↓ ↓H␈↓ε ((LAMBDA(N)
␈↓ ↓H␈↓ε (APPEND (COMPLIS (CDR EXP) M VPR)
␈↓ ↓H␈↓ε (LOADAC (DIFFERENCE 1 N) 1)
␈↓ ↓H␈↓ε (LIST (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE %) 0 0 N N)))
␈↓ ↓H␈↓ε (LIST (LIST (QUOTE CALL) N (LIST (QUOTE QUOTE) (CAR EXP))))))
␈↓ ↓H␈↓ε (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ε ((EQ (CAAR EXP) (QUOTE LAMBDA))
␈↓ ↓H␈↓ε ((LAMBDA(N)
␈↓ ↓H␈↓ε (APPEND (COMPLIS (CDR EXP) M VPR)
␈↓ ↓H␈↓ε (COMPEXP (CADDAR EXP) (DIFFERENCE M N) (APPEND (PRUP (CADAR EXP) (DIFFERENCE 1 M)) VPR))
␈↓ ↓H␈↓ε (LIST (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE %) 0 0 N N)))))
␈↓ ↓H␈↓ε (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ε (T NIL)))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP COMPLIS
␈↓ ↓H␈↓ε (LAMBDA(U M VPR)
␈↓ ↓H␈↓ε (COND ((NULL U) NIL)
␈↓ ↓H␈↓ε (T (APPEND (COMPEXP (CAR U) M VPR) (QUOTE ((PUSH P 1))) (COMPLIS (CDR U) (SUB1 M) VPR)))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP LOADAC
␈↓ ↓H␈↓ε (LAMBDA(N K)
␈↓ ↓H␈↓ε (COND ((GREATERP N 0) NIL) (T (CONS (LIST (QUOTE MOVE) K N (QUOTE P)) (LOADAC (ADD1 N) (ADD1 K))))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP COMCOND
␈↓ ↓H␈↓ε (LAMBDA(U M L VPR)
␈↓ ↓H␈↓ε (COND ((NULL U) (LIST L))
␈↓ ↓H␈↓ε (T
␈↓ ↓H␈↓ε ((LAMBDA(L1)
␈↓ ↓H␈↓ε (APPEND (COMBOOL (CAAR U) M L1 NIL VPR)
␈↓ ↓H␈↓ε (COMPEXP (CADAR U) M VPR)
␈↓ ↓H␈↓ε (LIST (LIST (QUOTE JRST) 0 L) L1)
␈↓ ↓H␈↓ε (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ε (GENSYM)))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP COMBOOL
␈↓ ↓H␈↓ε (LAMBDA(P M L FLG VPR)
␈↓ ↓H␈↓ε (COND ((ATOM P) (APPEND (COMPEXP P M VPR) (LIST (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE))) 1 L))))
␈↓ ↓H␈↓ε ((EQ (CAR P) (QUOTE AND))
␈↓ ↓H␈↓ε (COND ((NOT FLG) (COMPANDOR (CDR P) M L NIL VPR))
␈↓ ↓H␈↓ε (T
␈↓ ↓H␈↓ε ((LAMBDA(L1)
␈↓ ↓H␈↓ε (APPEND (COMPANDOR (CDR P) M L1 NIL VPR) (LIST (LIST (QUOTE JRST) 0 L)) (LIST L1)))
␈↓ ↓H␈↓ε (GENSYM)))))
␈↓ ↓H␈↓ε ((EQ (CAR P) (QUOTE OR))
␈↓ ↓H␈↓ε (COND (FLG (COMPANDOR (CDR P) M L T VPR))
␈↓ ↓H␈↓ε (T
␈↓ ↓H␈↓ε ((LAMBDA (L1) (APPEND (COMPANDOR (CDR P) M L1 T VPR) (LIST (LIST (QUOTE JRST) 0 L)) (LIST L1)))
␈↓ ↓H␈↓ε (GENSYM)))))
␈↓ ↓H␈↓ε ((EQ (CAR P) (QUOTE NOT)) (COMBOOL (CADR P) M L (NOT FLG) VPR))
␈↓ ↓H␈↓ε (T (APPEND (COMPEXP P M VPR) (LIST (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE))) 1 L))))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP COMPANDOR
␈↓ ↓H␈↓ε (LAMBDA(U M L FLG VPR)
␈↓ ↓H␈↓ε (COND ((NULL U) NIL) (T (APPEND (COMBOOL (CAR U) M L FLG VPR) (COMPANDOR (CDR U) M L FLG VPR)))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓␈↓ ¬qAppendix B␈↓ βxxvii
␈↓ ↓H␈↓2. ␈↓αMACLISP listing of LCOM4␈↓
␈↓ ↓H␈↓ε(DECLARE (SETQ NO-DISK-HACKS T))
␈↓ ↓H␈↓ε(DECLARE (READ))
␈↓ ↓H␈↓ε(DEFPROP COMPFCNS
␈↓ ↓H␈↓ε (COMPFCNS COMPL
␈↓ ↓H␈↓ε COMP
␈↓ ↓H␈↓ε SUBSTACK
␈↓ ↓H␈↓ε PRUP
␈↓ ↓H␈↓ε MKPUSH
␈↓ ↓H␈↓ε COMPEXP
␈↓ ↓H␈↓ε STACKUP
␈↓ ↓H␈↓ε CCCHAIN
␈↓ ↓H␈↓ε COMPC
␈↓ ↓H␈↓ε COMCOND
␈↓ ↓H␈↓ε COMPLISA
␈↓ ↓H␈↓ε CCOUNT
␈↓ ↓H␈↓ε LOADAC
␈↓ ↓H␈↓ε COMPLIS
␈↓ ↓H␈↓ε CLASSIFY
␈↓ ↓H␈↓ε CLASS1
␈↓ ↓H␈↓ε CLASS2
␈↓ ↓H␈↓ε MKJRST
␈↓ ↓H␈↓ε COMBOOL
␈↓ ↓H␈↓ε COMPANDOR
␈↓ ↓H␈↓ε COMPANDOR1
␈↓ ↓H␈↓ε FLAT)
␈↓ ↓H␈↓εVALUE)
␈↓ ↓H␈↓ε(DEFPROP COMPL
␈↓ ↓H␈↓ε (LAMBDA(FILE)
␈↓ ↓H␈↓ε (UWRITE)
␈↓ ↓H␈↓ε (APPLY (QUOTE EREAD) FILE)
␈↓ ↓H␈↓ε (SELECT-DISK-INPUT
␈↓ ↓H␈↓ε (READ-UNTIL-EOF
␈↓ ↓H␈↓ε WITH
␈↓ ↓H␈↓ε Z
␈↓ ↓H␈↓ε DO
␈↓ ↓H␈↓ε (COND ((OR (EQ (CAR Z) (QUOTE DEFUN)) (AND (EQ (CAR Z) (QUOTE DEFPROP)) (EQ (CADDDR Z) (QUOTE EXPR))))
␈↓ ↓H␈↓ε (PROG (PROG)
␈↓ ↓H␈↓ε (SETQ PROG
␈↓ ↓H␈↓ε (COND ((EQ (CAR Z) (QUOTE DEFUN)) (COMP (CADR Z) (CADDR Z) (CADDDR Z)))
␈↓ ↓H␈↓ε (T (COMP (CADR Z) (CADR (CADDR Z)) (CADDR (CADDR Z))))))
␈↓ ↓H␈↓ε (UNSELECT-TTY (SELECT-DISK-OUTPUT (MAPC (FUNCTION PRINT) PROG)))
␈↓ ↓H␈↓ε (PRINT (LIST (CADR Z) (LENGTH PROG)))))
␈↓ ↓H␈↓ε (T (UNSELECT-TTY (SELECT-DISK-OUTPUT (PRINT Z))))))
␈↓ ↓H␈↓ε (APPLY (QUOTE UFILE) (LIST (CAR FILE) (QUOTE LAP)))
␈↓ ↓H␈↓ε (QUOTE ENDCOMP)))
␈↓ ↓H␈↓εFEXPR)
␈↓ ↓H␈↓ε(DEFPROP COMP
␈↓ ↓H␈↓ε (LAMBDA(FN VARS EXP)
␈↓ ↓H␈↓ε ((LAMBDA(VPR N)
␈↓ ↓H␈↓ε (FLAT (LIST (LIST (LIST (QUOTE LAP) FN (QUOTE SUBR)))
␈↓ ↓H␈↓ε (MKPUSH N 1)
␈↓ ↓H␈↓ε (COMPEXP EXP (MINUS N) VPR)
␈↓ ↓H␈↓ε (SUBSTACK N)
␈↓ ↓H␈↓ε (QUOTE ((POPJ P) (LABEL NIL))))
␈↓ ↓H␈↓ε NIL))
␈↓ ↓H␈↓ε (PRUP VARS 1)
␈↓ ↓H␈↓ε (LENGTH VARS)))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP SUBSTACK
␈↓ ↓H␈↓ε (LAMBDA (N) (COND ((= N 0) NIL) (T (LIST (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE %) 0 0 N N))))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓xxviii␈↓ ¬qAppendix B␈↓ H
␈↓ ↓H␈↓ε(DEFPROP PRUP
␈↓ ↓H␈↓ε (LAMBDA (VARS N) (COND ((NULL VARS) NIL) (T (CONS (CONS (CAR VARS) N) (PRUP (CDR VARS) (ADD1 N))))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP MKPUSH
␈↓ ↓H␈↓ε (LAMBDA (N M) (COND ((LESSP N M) NIL) (T (CONS (LIST (QUOTE PUSH) (QUOTE P) M) (MKPUSH N (ADD1 M))))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP COMPEXP
␈↓ ↓H␈↓ε (LAMBDA(EXP M VPR)
␈↓ ↓H␈↓ε (COND ((NULL EXP) (QUOTE ((MOVEI 1 0))))
␈↓ ↓H␈↓ε ((OR (EQ EXP T) (NUMBERP EXP)) (LIST (LIST (QUOTE MOVEI) 1 (LIST (QUOTE QUOTE) EXP))))
␈↓ ↓H␈↓ε ((ATOM EXP) (LIST (LIST (QUOTE MOVE) 1 (PLUS M (CDR (ASSOC EXP VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε ((EQ (CAR EXP) (QUOTE CAR))
␈↓ ↓H␈↓ε (COND ((ATOM (CADR EXP))
␈↓ ↓H␈↓ε (LIST (LIST (QUOTE HLRZ) 1 (QUOTE @)(PLUS M (CDR (ASSOC (CADR EXP) VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε (T (LIST (COMPEXP (CADR EXP) M VPR) (QUOTE ((HLRZ 1 @ 1)))))))
␈↓ ↓H␈↓ε ((EQ (CAR EXP) (QUOTE CDR))
␈↓ ↓H␈↓ε (COND ((ATOM (CADR EXP))
␈↓ ↓H␈↓ε (LIST (LIST (QUOTE HRRZ) 1 (QUOTE @)(PLUS M (CDR (ASSOC (CADR EXP) VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε (T (LIST (COMPEXP (CADR EXP) M VPR) (QUOTE ((HRRZ 1 @ 1)))))))
␈↓ ↓H␈↓ε ((OR (EQ (CAR EXP) (QUOTE AND))
␈↓ ↓H␈↓ε (EQ (CAR EXP) (QUOTE OR))
␈↓ ↓H␈↓ε (EQ (CAR EXP) (QUOTE NOT))
␈↓ ↓H␈↓ε (EQ (CAR EXP) (QUOTE EQ)))
␈↓ ↓H␈↓ε ((LAMBDA(L1 L2)
␈↓ ↓H␈↓ε (LIST (COMBOOL EXP M L1 NIL VPR)
␈↓ ↓H␈↓ε (LIST (QUOTE (MOVEI 1 (QUOTE T)))
␈↓ ↓H␈↓ε (LIST (QUOTE JRST) 0 L2)
␈↓ ↓H␈↓ε (LIST (QUOTE LABEL) L1)
␈↓ ↓H␈↓ε (QUOTE (MOVEI 1 0))
␈↓ ↓H␈↓ε (LIST (QUOTE LABEL) L2))))
␈↓ ↓H␈↓ε (GENSYM)
␈↓ ↓H␈↓ε (GENSYM)))
␈↓ ↓H␈↓ε ((EQ (CAR EXP) (QUOTE COND)) (COMCOND (CDR EXP) M (GENSYM) VPR))
␈↓ ↓H␈↓ε ((EQ (CAR EXP) (QUOTE QUOTE)) (LIST (LIST (QUOTE MOVEI) 1 EXP)))
␈↓ ↓H␈↓ε ((ATOM (CAR EXP))
␈↓ ↓H␈↓ε (LIST (COMPLISA (CDR EXP) M VPR)
␈↓ ↓H␈↓ε (LIST (LIST (QUOTE CALL) (LENGTH (CDR EXP)) (LIST (QUOTE QUOTE) (CAR EXP))))))
␈↓ ↓H␈↓ε ((EQ (CAAR EXP) (QUOTE LAMBDA))
␈↓ ↓H␈↓ε ((LAMBDA(N)
␈↓ ↓H␈↓ε (LIST (STACKUP (CDR EXP) M VPR)
␈↓ ↓H␈↓ε (COMPEXP (CADDAR EXP) (DIFFERENCE M N) (APPEND (PRUP (CADAR EXP) (DIFFERENCE 1 M)) VPR))
␈↓ ↓H␈↓ε (SUBSTACK N)))
␈↓ ↓H␈↓ε (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ε (T NIL)))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP STACKUP
␈↓ ↓H␈↓ε (LAMBDA(U M VPR)
␈↓ ↓H␈↓ε (COND ((NULL U) NIL) (T (LIST (COMPEXP (CAR U) M VPR) (QUOTE ((PUSH P 1))) (STACKUP (CDR U) (SUB1 M) VPR)))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP CCCHAIN
␈↓ ↓H␈↓ε (LAMBDA(EXP)
␈↓ ↓H␈↓ε (AND (OR (EQ (CAR EXP) (QUOTE CAR)) (EQ (CAR EXP) (QUOTE CDR))) (OR (ATOM (CADR EXP)) (CCCHAIN (CADR EXP)))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP COMPC
␈↓ ↓H␈↓ε (LAMBDA(EXP N2 M VPR)
␈↓ ↓H␈↓ε (COND ((ATOM EXP) (ERROR (QUOTE COMPC)))
␈↓ ↓H␈↓ε ((EQ (CAR EXP) (QUOTE CAR))
␈↓ ↓H␈↓ε (COND ((ATOM (CADR EXP))
␈↓ ↓H␈↓ε (LIST (LIST (QUOTE HLRZ) N2 (QUOTE @)(PLUS M (CDR (ASSOC (CADR EXP) VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε (T (CONS (LIST (QUOTE HLRZ) N2 (QUOTE @) N2) (COMPC (CADR EXP) N2 M VPR)))))
␈↓ ↓H␈↓ε ((ATOM (CADR EXP)) (LIST (LIST (QUOTE HRRZ) (QUOTE @) N2 (PLUS M (CDR (ASSOC (CADR EXP) VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε (T (CONS (LIST (QUOTE HRRZ) N2 (QUOTE @) N2) (COMPC (CADR EXP) N2 M VPR)))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓␈↓ ¬qAppendix B␈↓ xxix
␈↓ ↓H␈↓ε(DEFPROP COMCOND
␈↓ ↓H␈↓ε (LAMBDA(U M L VPR)
␈↓ ↓H␈↓ε (COND ((NULL U) (LIST (LIST (QUOTE LABEL) L)))
␈↓ ↓H␈↓ε ((AND (NOT (ATOM (CAAR U))) (EQ (CAAAR U) (QUOTE NULL)) (NULL (CADAR U)))
␈↓ ↓H␈↓ε (LIST (COMPEXP (CADAAR U) M VPR) (LIST (LIST (QUOTE JUMPE) 1 L)) (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ε ((EQ (CAAR U) T) (LIST (COMPEXP (CADAR U) M VPR) (LIST (LIST (QUOTE LABEL) L))))
␈↓ ↓H␈↓ε (T
␈↓ ↓H␈↓ε ((LAMBDA(L1)
␈↓ ↓H␈↓ε (LIST (COMBOOL (CAAR U) M L1 NIL VPR)
␈↓ ↓H␈↓ε (COMPEXP (CADAR U) M VPR)
␈↓ ↓H␈↓ε (LIST (LIST (QUOTE JRST) 0 L) (LIST (QUOTE LABEL) L1))
␈↓ ↓H␈↓ε (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ε (GENSYM)))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP COMPLISA
␈↓ ↓H␈↓ε (LAMBDA(U M VPR)
␈↓ ↓H␈↓ε ((LAMBDA(Z)
␈↓ ↓H␈↓ε (LIST (COMPLIS Z M 1 VPR)
␈↓ ↓H␈↓ε (LOADAC Z (DIFFERENCE 1 (CCOUNT Z)) 1 (DIFFERENCE M (CCOUNT Z)) VPR)
␈↓ ↓H␈↓ε (SUBSTACK (CCOUNT Z))))
␈↓ ↓H␈↓ε (CLASSIFY U)))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP CCOUNT
␈↓ ↓H␈↓ε (LAMBDA (Z) (COND ((NULL Z) 0) ((= (CAAR Z) 4) (ADD1 (CCOUNT (CDR Z)))) (T (CCOUNT (CDR Z)))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP LOADAC
␈↓ ↓H␈↓ε (LAMBDA(Z M2 N2 M VPR)
␈↓ ↓H␈↓ε (COND ((NULL Z) NIL)
␈↓ ↓H␈↓ε ((= (CAAR Z) 1)
␈↓ ↓H␈↓ε (CONS (LIST (QUOTE MOVE) N2 (PLUS M (CDR (ASSOC (CDAR Z) VPR))) (QUOTE P))
␈↓ ↓H␈↓ε (LOADAC (CDR Z) M2 (ADD1 N2) M VPR)))
␈↓ ↓H␈↓ε ((= (CAAR Z) 0)
␈↓ ↓H␈↓ε (CONS (LIST (QUOTE MOVEI) N2 (LIST (QUOTE QUOTE) (CDAR Z))) (LOADAC (CDR Z) M2 (ADD1 N2) M VPR)))
␈↓ ↓H␈↓ε ((= (CAAR Z) 2) (CONS (LIST (QUOTE MOVEI) N2 (CDAR Z)) (LOADAC (CDR Z) M2 (ADD1 N2) M VPR)))
␈↓ ↓H␈↓ε ((= (CAAR Z) 3) (LIST (REVERSE (COMPC (CDAR Z) N2 M VPR)) (LOADAC (CDR Z) M2 (ADD1 N2) M VPR)))
␈↓ ↓H␈↓ε ((= (CAAR Z) 5) (LOADAC (CDR Z) 1 (ADD1 N2) M VPR))
␈↓ ↓H␈↓ε (T (CONS (LIST (QUOTE MOVE) N2 M2 (QUOTE P)) (LOADAC (CDR Z) (ADD1 M2) (ADD1 N2) M VPR)))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP COMPLIS
␈↓ ↓H␈↓ε (LAMBDA(Z M K VPR)
␈↓ ↓H␈↓ε (COND ((NULL Z) NIL)
␈↓ ↓H␈↓ε ((= (CAAR Z) 4)
␈↓ ↓H␈↓ε (LIST (COMPEXP (CDAR Z) M VPR) (QUOTE ((PUSH P 1))) (COMPLIS (CDR Z) (SUB1 M) (ADD1 K) VPR)))
␈↓ ↓H␈↓ε ((= (CAAR Z) 5) (LIST (COMPEXP (CDAR Z) M VPR) (COND ((= K 1) NIL) (T (LIST (LIST (QUOTE MOVE) K 1))))))
␈↓ ↓H␈↓ε (T (COMPLIS (CDR Z) M (ADD1 K) VPR))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP CLASSIFY
␈↓ ↓H␈↓ε (LAMBDA (U) (CLASS2 (CLASS1 U NIL) NIL T))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP CLASS1
␈↓ ↓H␈↓ε (LAMBDA(U V)
␈↓ ↓H␈↓ε (COND ((NULL U) V)
␈↓ ↓H␈↓ε ((ATOM (CAR U))
␈↓ ↓H␈↓ε (COND ((OR (EQUAL (CAR U) NIL) (EQUAL (CAR U) T) (NUMBERP (CAR U)))
␈↓ ↓H␈↓ε (CLASS1 (CDR U) (CONS (CONS 0 (CAR U)) V)))
␈↓ ↓H␈↓ε (T (CLASS1 (CDR U) (CONS (CONS 1 (CAR U)) V)))))
␈↓ ↓H␈↓ε ((EQUAL (CAAR U) (QUOTE QUOTE)) (CLASS1 (CDR U) (CONS (CONS 2 (CAR U)) V)))
␈↓ ↓H␈↓ε ((CCCHAIN (CAR U)) (CLASS1 (CDR U) (CONS (CONS 3 (CAR U)) V)))
␈↓ ↓H␈↓ε (T (CLASS1 (CDR U) (CONS (CONS 4 (CAR U)) V)))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP CLASS2
␈↓ ↓H␈↓ε (LAMBDA(U V FLG)
␈↓ ↓H␈↓xxx␈↓ ¬qAppendix B␈↓ H
␈↓ ↓H␈↓ε (COND ((NULL U) V)
␈↓ ↓H␈↓ε ((AND FLG (= (CAAR U) 4)) (CLASS2 (CDR U) (CONS (CONS 5 (CDAR U)) V) NIL))
␈↓ ↓H␈↓ε (T (CLASS2 (CDR U) (CONS (CAR U) V) FLG))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP MKJRST
␈↓ ↓H␈↓ε (LAMBDA (L) (LIST (LIST (QUOTE JRST) 0 L)))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP COMBOOL
␈↓ ↓H␈↓ε (LAMBDA(P M L FLG VPR)
␈↓ ↓H␈↓ε (COND ((EQ P T) (COND (FLG (MKJRST L)) (T NIL)))
␈↓ ↓H␈↓ε ((ATOM P) (LIST (COMPEXP P M VPR) (LIST (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE))) 1 L))))
␈↓ ↓H␈↓ε ((EQ (CAR P) (QUOTE EQ))
␈↓ ↓H␈↓ε (LIST (COMPLISA (CDR P) M VPR) (COND (FLG (QUOTE ((CAMN 1 2)))) (T (QUOTE ((CAME 1 2))))) (MKJRST L)))
␈↓ ↓H␈↓ε ((EQ (CAR P) (QUOTE AND))
␈↓ ↓H␈↓ε (COND ((NOT FLG) (COMPANDOR (CDR P) M L NIL VPR))
␈↓ ↓H␈↓ε (T
␈↓ ↓H␈↓ε ((LAMBDA (L1) (LIST (COMPANDOR1 (CDR P) M L1 L NIL VPR) (LIST (LIST (QUOTE LABEL) L1))))
␈↓ ↓H␈↓ε (GENSYM)))))
␈↓ ↓H␈↓ε ((EQ (CAR P) (QUOTE OR))
␈↓ ↓H␈↓ε (COND (FLG (COMPANDOR (CDR P) M L T VPR))
␈↓ ↓H␈↓ε (T
␈↓ ↓H␈↓ε ((LAMBDA (L1) (LIST (COMPANDOR1 (CDR P) M L1 L T VPR) (LIST (LIST (QUOTE LABEL) L1))))
␈↓ ↓H␈↓ε (GENSYM)))))
␈↓ ↓H␈↓ε ((EQ (CAR P) (QUOTE NOT)) (COMBOOL (CADR P) M L (NOT FLG) VPR))
␈↓ ↓H␈↓ε ((EQ (CAR P) (QUOTE NULL))
␈↓ ↓H␈↓ε (LIST (COMPEXP (CADR P) M VPR) (LIST (LIST (COND (FLG (QUOTE JUMPE)) (T (QUOTE JUMPN))) 1 L))))
␈↓ ↓H␈↓ε (T (LIST (COMPEXP P M VPR) (LIST (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE))) 1 L))))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP COMPANDOR
␈↓ ↓H␈↓ε (LAMBDA(U M L FLG VPR)
␈↓ ↓H␈↓ε (COND ((NULL U) NIL) (T (LIST (COMBOOL (CAR U) M L FLG VPR) (COMPANDOR (CDR U) M L FLG VPR)))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP COMPANDOR1
␈↓ ↓H␈↓ε (LAMBDA(U M L L2 FLG VPR)
␈↓ ↓H␈↓ε (COND ((NULL U) (MKJRST L2))
␈↓ ↓H␈↓ε ((NULL (CDR U)) (COMBOOL (CAR U) M L2 (NOT FLG) VPR))
␈↓ ↓H␈↓ε (T (LIST (COMBOOL (CAR U) M L FLG VPR) (COMPANDOR1 (CDR U) M L L2 FLG VPR)))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓ε(DEFPROP FLAT
␈↓ ↓H␈↓ε (LAMBDA(U S)
␈↓ ↓H␈↓ε (COND ((NULL U) S)
␈↓ ↓H␈↓ε ((NULL (CAR U)) (FLAT (CDR U) S))
␈↓ ↓H␈↓ε ((EQ (CAR U) (QUOTE LABEL)) (CONS (CADR U) S))
␈↓ ↓H␈↓ε ((ATOM (CAR U)) (CONS U S))
␈↓ ↓H␈↓ε (T (FLAT (CAR U) (FLAT (CDR U) S)))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓␈↓ εH␈↓ ?i
␈↓ ↓H␈↓α␈↓ ¬=FUNCTION INDEX
␈↓ ↓H␈↓allsol 35 ␈↓ εh␈↓glub 21
␈↓ ↓H␈↓alt 12 ␈↓ εh␈↓gopher 66
␈↓ ↓H␈↓and 11 ␈↓ εh␈↓imval 45
␈↓ ↓H␈↓andlis 20 ␈↓ εh␈↓insertb 86
␈↓ ↓H␈↓answer 47 ␈↓ εh␈↓last 13
␈↓ ↓H␈↓append 14, 82, 83 ␈↓ εh␈↓left 87
␈↓ ↓H␈↓assoc 23 ␈↓ εh␈↓length 22
␈↓ ↓H␈↓bestmin 37 ␈↓ εh␈↓linemax 39
␈↓ ↓H␈↓bextmax 37 ␈↓ εh␈↓linemin 40
␈↓ ↓H␈↓ccchain 129 ␈↓ εh␈↓listsubt 43
␈↓ ↓H␈↓ccount 130 ␈↓ εh␈↓loadac 125, 130
␈↓ ↓H␈↓class1 131 ␈↓ εh␈↓loop 134
␈↓ ↓H␈↓class2 131 ␈↓ εh␈↓lose 32, 33, 35
␈↓ ↓H␈↓classify 130 ␈↓ εh␈↓mapapp 35
␈↓ ↓H␈↓combool 125, 131 ␈↓ εh␈↓mapcar 18
␈↓ ↓H␈↓comcond 125, 129 ␈↓ εh␈↓mapcar2 33
␈↓ ↓H␈↓commence 44 ␈↓ εh␈↓mapchoose 35
␈↓ ↓H␈↓commonhead 43 ␈↓ εh␈↓maplist 18
␈↓ ↓H␈↓commontail 43 ␈↓ εh␈↓maxlis 36, 37
␈↓ ↓H␈↓comp 123, 127 ␈↓ εh␈↓member 15
␈↓ ↓H␈↓compandor 126, 131 ␈↓ εh␈↓minlis 36
␈↓ ↓H␈↓compandor1 132 ␈↓ εh␈↓mkjrst 131
␈↓ ↓H␈↓compc 129 ␈↓ εh␈↓mkpush 124, 128
␈↓ ↓H␈↓compexp 124, 128 ␈↓ εh␈↓mod 16
␈↓ ↓H␈↓compile 110 ␈↓ εh␈↓movemax 37
␈↓ ↓H␈↓compl 123 ␈↓ εh␈↓movemin 37
␈↓ ↓H␈↓complis 125, 130 ␈↓ εh␈↓nconc 85
␈↓ ↓H␈↓complisa 129 ␈↓ εh␈↓neval 133
␈↓ ↓H␈↓coompl 127 ␈↓ εh␈↓nevcond 134
␈↓ ↓H␈↓cycles 34 ␈↓ εh␈↓newgame 45
␈↓ ↓H␈↓delete 47 ␈↓ εh␈↓not 11
␈↓ ↓H␈↓diff 19 ␈↓ εh␈↓nprup 134
␈↓ ↓H␈↓doubleth 47 ␈↓ εh␈↓nth 33
␈↓ ↓H␈↓equal 14 ␈↓ εh␈↓numval 24
␈↓ ↓H␈↓equiv 89 ␈↓ εh␈↓or 11
␈↓ ↓H␈↓eval 25 ␈↓ εh␈↓orlis 20
␈↓ ↓H␈↓evcond 25 ␈↓ εh␈↓outcome 110
␈↓ ↓H␈↓evlist 25 ␈↓ εh␈↓poo 34
␈↓ ↓H␈↓evplus 24 ␈↓ εh␈↓prina 92
␈↓ ↓H␈↓evtimes 24 ␈↓ εh␈↓prinb 92
␈↓ ↓H␈↓ext 45 ␈↓ εh␈↓prup 25, 123, 128
␈↓ ↓H␈↓fact 16 ␈↓ εh␈↓read 93
␈↓ ↓H␈↓flat 128 ␈↓ εh␈↓rectify 43
␈↓ ↓H␈↓flatten 15 ␈↓ εh␈↓reverse 15, 81
␈↓ ↓H␈↓fringe 16, 65 ␈↓ εh␈↓reversea 15
␈↓ ↓H␈↓gcd 16 ␈↓ εh␈↓revert 46
␈↓ ↓H␈↓ii␈↓ ¬9FUNCTION INDEX␈↓ H
␈↓ ↓H␈↓same 66
␈↓ ↓H␈↓samea 66
␈↓ ↓H␈↓samefringe 66, 70
␈↓ ↓H␈↓samefringea 66
␈↓ ↓H␈↓search 32
␈↓ ↓H␈↓searchlis 32
␈↓ ↓H␈↓simplify 103
␈↓ ↓H␈↓size 67
␈↓ ↓H␈↓sort 46
␈↓ ↓H␈↓stackup 129
␈↓ ↓H␈↓step 109
␈↓ ↓H␈↓subst 14
␈↓ ↓H␈↓substack 128
␈↓ ↓H␈↓successors 32, 33, 35, 46
␈↓ ↓H␈↓ta 135
␈↓ ↓H␈↓ter 32, 33, 35, 45
␈↓ ↓H␈↓terma 135
␈↓ ↓H␈↓termastar 135
␈↓ ↓H␈↓threat 47
␈↓ ↓H␈↓t␈↓β1␈↓ 135
␈↓ ↓H␈↓treemax 41
␈↓ ↓H␈↓treemin 41
␈↓ ↓H␈↓twolis 47
␈↓ ↓H␈↓update 46
␈↓ ↓H␈↓upto 34
␈↓ ↓H␈↓valmax 39
␈↓ ↓H␈↓valmin 39
␈↓ ↓H␈↓value 103
␈↓ ↓H␈↓vmax 37
␈↓ ↓H␈↓vmin 37
␈↓ ↓H␈↓win 47
␈↓ ↓H␈↓zap 34